generated from bywhitebird/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from bywhitebird/72-add-github-authentication-…
…to-alakazam feat(Alakazam): add github auth and pages and api routes for auth
- Loading branch information
Showing
8 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<script setup lang="ts"> | ||
import Button from "@whitebird/ui/vue/button" | ||
const { loggedIn, user, session, clear: clearSession, fetch: fetchSession } = useUserSession() | ||
async function login() { | ||
await navigateTo(`/api/auth/github`, { | ||
external: true, | ||
open: { | ||
target: '_blank', | ||
}, | ||
}) | ||
} | ||
function logout() { | ||
clearSession() | ||
} | ||
onMounted(() => { | ||
window.addEventListener( | ||
"message", | ||
(event) => { | ||
if (event.data.type === "auth") { | ||
fetchSession() | ||
} | ||
}, | ||
false, | ||
); | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
:class="css({ | ||
bg: 'appBackground', | ||
color: 'highContrastForeground', | ||
height: '100dvh', | ||
width: '100dvw', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'safe center', | ||
overflow: 'auto', | ||
scrollbarGutter: 'stable', | ||
})" | ||
> | ||
<Button | ||
text="Login with GitHub" | ||
variant="primary" | ||
icon-name="github" | ||
@click="login" | ||
/> | ||
<Button | ||
text="Logout" | ||
variant="secondary" | ||
icon-name="logout-box" | ||
@click="logout" | ||
/> | ||
<pre>{{ loggedIn }}</pre> | ||
<pre>{{ session }}</pre> | ||
<pre>{{ user }}</pre> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineNuxtRoutes } from "~/shared/utils/define-nuxt-routes"; | ||
|
||
export default defineNuxtRoutes(__dirname, [ | ||
{ | ||
name: 'login-page', | ||
path: '/login', | ||
file: './pages/login.vue', | ||
}, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export default oauth.githubEventHandler({ | ||
config: { | ||
scope: ['repo'], | ||
}, | ||
async onSuccess(event, data) { | ||
await setUserSession(event, { | ||
user: { | ||
provider: 'github', | ||
token: data.tokens.access_token, | ||
id: data.user.id, | ||
login: data.user.login, | ||
name: data.user.name, | ||
avatarUrl: data.user.avatar_url, | ||
}, | ||
loggedInAt: Date.now(), | ||
}) | ||
|
||
setResponseHeaders(event, { | ||
'content-type': 'text/html', | ||
}) | ||
return ` | ||
<script> | ||
window.opener.postMessage({ | ||
type: 'auth', | ||
status: 'success', | ||
provider: 'github', | ||
}) | ||
window.close() | ||
</script> | ||
` as never as void | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "../.nuxt/tsconfig.server.json" | ||
"extends": "../../.nuxt/tsconfig.server.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare module '#auth-utils' { | ||
interface UserSession { | ||
loggedInAt: number | ||
user: ( | ||
| { | ||
provider: 'github' | ||
token: string | ||
id: number | ||
login: string | ||
name: string | ||
avatarUrl: string | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.