Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cleaning up oauth changes #118

Merged
merged 2 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/components/Browse/LoginPrompt.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import IconSocialDiscordInverse from '$lib/assets/icons/social/IconSocialDiscordInverse.svelte'
import IconSocialGoogle from '$lib/assets/icons/social/IconSocialGoogle.svelte'
import IconSocialGitHubInverse from '$lib/assets/icons/social/IconSocialGitHubInverse.svelte'
import { getGitHubUrl, getDiscordUrl, getGoogleUrl } from '$lib/utils/authUrl'
// import { getGitHubUrl, getDiscordUrl, getGoogleUrl } from '$lib/utils/authUrl'
import { env } from '$env/dynamic/public'

async function getHref(provider: string) {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function logout() {
window.location.href = '/'
}


function getJWT() {
return window.localStorage.getItem(JWT_KEY)
}

function getUserId() {
return window.localStorage.getItem('userId')
}

function setJWT({ jwt }: { jwt: string }) {
window.localStorage.setItem(JWT_KEY, jwt)
}
Expand Down Expand Up @@ -51,6 +60,8 @@ async function me() {

export {
logout,
getJWT,
getUserId,
setJWT,
setUserId,
setUser,
Expand Down
120 changes: 60 additions & 60 deletions src/lib/utils/authUrl.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import {
PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
PUBLIC_GITHUB_OAUTH_CLIENT_ID,
PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
PUBLIC_DISCORD_OAUTH_CLIENT_ID,
PUBLIC_DISCORD_OAUTH_REDIRECT_URL
} from '$env/static/public'

export const getGoogleUrl = (from: string) => {
const rootUrl = `https://accounts.google.com/o/oauth2/v2/auth`

const options = {
client_id: PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
access_type: 'offline',
response_type: 'code',
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
].join(' '),
state: from
}

const qs = new URLSearchParams(options)

return `${rootUrl}?${qs.toString()}`
}

export const getGitHubUrl = (from: string) => {
const rootURl = 'https://github.com/login/oauth/authorize'

const options = {
client_id: PUBLIC_GITHUB_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
scope: 'user:email',
state: from
}

const qs = new URLSearchParams(options)

return `${rootURl}?${qs.toString()}`
}

export const getDiscordUrl = (from: string) => {
const rootURl = 'https://discord.com/api/oauth2/authorize'

const options = {
client_id: PUBLIC_DISCORD_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_DISCORD_OAUTH_REDIRECT_URL,
scope: 'identify email',
response_type: 'code',
state: from
}

const qs = new URLSearchParams(options)

return `${rootURl}?${qs.toString()}`
}
// import {
// PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
// PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
// PUBLIC_GITHUB_OAUTH_CLIENT_ID,
// PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
// PUBLIC_DISCORD_OAUTH_CLIENT_ID,
// PUBLIC_DISCORD_OAUTH_REDIRECT_URL
// } from '$env/static/public'

// export const getGoogleUrl = (from: string) => {
// const rootUrl = `https://accounts.google.com/o/oauth2/v2/auth`

// const options = {
// client_id: PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
// access_type: 'offline',
// response_type: 'code',
// prompt: 'consent',
// scope: [
// 'https://www.googleapis.com/auth/userinfo.profile',
// 'https://www.googleapis.com/auth/userinfo.email'
// ].join(' '),
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootUrl}?${qs.toString()}`
// }

// export const getGitHubUrl = (from: string) => {
// const rootURl = 'https://github.com/login/oauth/authorize'

// const options = {
// client_id: PUBLIC_GITHUB_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
// scope: 'user:email',
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootURl}?${qs.toString()}`
// }

// export const getDiscordUrl = (from: string) => {
// const rootURl = 'https://discord.com/api/oauth2/authorize'

// const options = {
// client_id: PUBLIC_DISCORD_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_DISCORD_OAUTH_REDIRECT_URL,
// scope: 'identify email',
// response_type: 'code',
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootURl}?${qs.toString()}`
// }
37 changes: 7 additions & 30 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// @ts-ignore
import NProgress from 'nprogress'
import { browser } from '$app/environment'
import { onMount } from 'svelte'
import { navigating } from '$app/stores'
// Auth store
import { authStore } from '$lib/stores/authStore'
import { getJWT, getUserId, me, setJWT, setUserId, currentUser } from '$lib/stores/authStore'

// NProgress Loading bar
import 'nprogress/nprogress.css'
Expand Down Expand Up @@ -46,7 +44,6 @@
}
}

const { currentUser } = authStore
export let data: any

$: data.user, storeUserData()
Expand All @@ -58,20 +55,20 @@
token = data.user.token
userId = data.user.userId
} else {
token = authStore.getJWT()
userId = authStore.getUserId()
token = getJWT()
userId = getUserId()
}

if (token || userId) {
authStore.setJWT({ jwt: token })
authStore.setUserId({ userId })
authStore.me()
setJWT({ jwt: token })
setUserId({ userId })
me()
}
}
}

function logout() {
authStore.logout()
logout()
}
</script>

Expand Down Expand Up @@ -253,23 +250,3 @@
</div>
</div>
<!-- {/if} -->

<!-- <style global>
:root {
--color-default: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--my-color: #25252a;
}
}
[data-theme='dark'] {
--color-default: #25252a;
}
[data-theme='light'] {
--my-color: #ffffff;
}
body {
background-color: var(--color-default);
}
</style> -->