Skip to content

Commit

Permalink
Merge pull request #76 from bywhitebird/72-add-github-authentication-…
Browse files Browse the repository at this point in the history
…to-alakazam

feat(Alakazam): add github auth and pages and api routes for auth
  • Loading branch information
arthur-fontaine authored Nov 12, 2023
2 parents dceabcc + 8017547 commit 5983422
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 34 deletions.
10 changes: 10 additions & 0 deletions apps/alakazam/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineNuxtConfig({

modules: [
'nuxt-typed-router',
'nuxt-auth-utils',
],

alias: {
Expand Down Expand Up @@ -38,4 +39,13 @@ export default defineNuxtConfig({
build: {
transpile: ['fsevents'],
},

runtimeConfig: {
oauth: {
github: {
clientId: process.env.NUXT_OAUTH_GITHUB_CLIENT_ID,
clientSecret: process.env.NUXT_OAUTH_GITHUB_CLIENT_SECRET,
}
}
}
})
1 change: 1 addition & 0 deletions apps/alakazam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"eslint-plugin-vue": "^9.17.0",
"glob": "^10.3.4",
"nuxt": "^3.7.1",
"nuxt-auth-utils": "^0.0.5",
"nuxt-typed-router": "^3.3.1"
}
}
66 changes: 66 additions & 0 deletions apps/alakazam/src/features/auth/pages/login.vue
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>
9 changes: 9 additions & 0 deletions apps/alakazam/src/features/auth/routes.ts
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',
},
])
32 changes: 32 additions & 0 deletions apps/alakazam/src/server/api/auth/github.get.ts
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
}
})
2 changes: 1 addition & 1 deletion apps/alakazam/src/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../.nuxt/tsconfig.server.json"
"extends": "../../.nuxt/tsconfig.server.json"
}
15 changes: 15 additions & 0 deletions apps/alakazam/src/types/auth.d.ts
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
}
)
}
}
Loading

0 comments on commit 5983422

Please sign in to comment.