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

Features/jwt local auth #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<script setup>
const { loggedIn, session, clear } = useUserSession()
const jwt = function(){
$fetch('/auth/jwt', {
body:{email:'admin@admin.com',password:'admin'}
,method:'POST'})
.then((res)=>{
session.value = res

})}
</script>

<template>
Expand Down Expand Up @@ -53,6 +61,17 @@ const { loggedIn, session, clear } = useUserSession()
>
Login with Twitch
</UButton>
<UButton
v-if="!loggedIn || !session.user.jwt"
icon="i-logos-jwt-icon"

external
color="gray"
size="xs"
@click="jwt"
>
Login with JWT
</UButton>
</template>
</UHeader>
<UMain>
Expand Down
1 change: 1 addition & 0 deletions playground/auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare module '#auth-utils' {
github?: any
google?: any
twitch?: any
jwt?: any
}
loggedInAt: number
}
Expand Down
15 changes: 15 additions & 0 deletions playground/server/routes/auth/jwt.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default local.jwtEventHandler({
config: {
},
async onSuccess (event, { user }) {
await setUserSession(event, {
user: {
jwt: user,

},
loggedInAt: Date.now()
})

return user
}
})
17 changes: 16 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { sha256 } from 'ohash'
import { defu } from 'defu'

// Module options TypeScript interface definition
export interface ModuleOptions {}
export interface ModuleOptions { }

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand Down Expand Up @@ -36,6 +36,10 @@ export default defineNuxtModule<ModuleOptions>({
from: resolver.resolve('./runtime/server/utils/oauth'),
imports: ['oauth']
},
{
from: resolver.resolve('./runtime/server/utils/local'),
imports: ['local']
},
{
from: resolver.resolve('./runtime/server/utils/session'),
imports: [
Expand Down Expand Up @@ -69,6 +73,7 @@ export default defineNuxtModule<ModuleOptions>({
})
// OAuth settings
runtimeConfig.oauth = defu(runtimeConfig.oauth, {})

// GitHub Oauth
runtimeConfig.oauth.github = defu(runtimeConfig.oauth.github, {
clientId: '',
Expand All @@ -89,5 +94,15 @@ export default defineNuxtModule<ModuleOptions>({
clientId: '',
clientSecret: ''
})

// Local settings
runtimeConfig.local = defu(runtimeConfig.local, {})

// JWT Local
runtimeConfig.local.jwt = defu(runtimeConfig.local.jwt, {
loginURL: '',
userURL: ''
})

}
})
63 changes: 63 additions & 0 deletions src/runtime/server/lib/local/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { H3Event, H3Error } from 'h3'
import { createError, eventHandler } from 'h3'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'

export interface JWTConfig {
/**
* GitHub OAuth Client ID
* @default process.env.NUXT_LOCAL_JWT_LOGIN_URL
*/
loginURL?: string

/**
* GitHub OAuth Client ID
* @default process.env.NUXT_LOCAL_JWT_USER_URL
*/
userURL?: string
}

interface JWTAuthConfig {
config?: JWTConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function jwtEventHandler ({ config, onSuccess, onError }: JWTAuthConfig) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).local?.jwt) as JWTConfig
console.log(config)
if (!config.loginURL || !config.userURL) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_LOCAL_JWT_LOGIN_URL or NUXT_LOCAL_JWT_USER_URL env variables.'
})
if (!onError) throw error
return onError(event, error)
}
const body = await readBody(event)


const tokens: any = await ofetch(config.loginURL, {
method: 'POST',
body
})

const user: any = await ofetch(
config.userURL,
{
headers: {
Authorization: `Bearer ${tokens.token}`,
},
}
)


return onSuccess(event, {
user,
tokens
})
})
}
5 changes: 5 additions & 0 deletions src/runtime/server/utils/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { jwtEventHandler } from '../lib/local/jwt'

export const local = {
jwtEventHandler
}