File tree 13 files changed +104
-46
lines changed
13 files changed +104
-46
lines changed Original file line number Diff line number Diff line change
1
+ AUTH0_BASE_URL =
2
+ AUTH0_ISSUER_BASE_URL =
3
+ AUTH0_CLIENT_ID =
4
+ AUTH0_CLIENT_SECRET =
5
+ AUTH0_AUDIENCE =
6
+ AUTH0_COOKIE_NAME =
7
+ AUTH0_DOMAIN =
8
+
Original file line number Diff line number Diff line change @@ -33,6 +33,8 @@ Make sure to install the dependencies
33
33
yarn install
34
34
```
35
35
36
+ Rename .env.example as .env and set environment values
37
+
36
38
## Development
37
39
38
40
Start the development server on http://localhost:3000
Original file line number Diff line number Diff line change 45
45
label: " About" ,
46
46
icon: " pi pi-fw pi-file" ,
47
47
to: " /about"
48
+ },
49
+ {
50
+ label: " Profile" ,
51
+ icon: " pi pi-fw pi-user" ,
52
+ to: " /profile"
48
53
}
49
54
];
50
55
</script >
Original file line number Diff line number Diff line change 1
1
import { defineNuxtConfig } from "nuxt" ;
2
2
3
3
const {
4
- AUTH0_SECRET ,
5
4
AUTH0_BASE_URL ,
6
5
AUTH0_ISSUER_BASE_URL ,
7
6
AUTH0_CLIENT_ID ,
8
7
AUTH0_CLIENT_SECRET ,
9
8
AUTH0_AUDIENCE ,
10
- AUTH0_COOKIE_NAME ,
9
+ AUTH0_COOKIE_NAME
11
10
} = process . env ;
12
11
13
12
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
14
13
export default defineNuxtConfig ( {
15
14
app : {
16
-
17
15
head : {
18
16
charset : "utf-8" ,
19
17
viewport : "width=device-width, initial-scale=1" ,
@@ -25,27 +23,25 @@ export default defineNuxtConfig({
25
23
content : "Nuxt3 + Auth0"
26
24
}
27
25
] ,
28
- link : [
29
- { rel : "icon" , type : "image/x-icon" , href : "/favicon.ico" }
30
- ]
31
- } ,
26
+ link : [ { rel : "icon" , type : "image/x-icon" , href : "/favicon.ico" } ]
27
+ }
32
28
} ,
33
29
typescript : {
34
- shim : false ,
30
+ shim : false
35
31
} ,
36
32
runtimeConfig : {
37
- AUTH0_SECRET ,
38
33
AUTH0_BASE_URL : AUTH0_BASE_URL || process . env . URL ,
39
34
AUTH0_ISSUER_BASE_URL ,
40
35
AUTH0_CLIENT_ID ,
41
36
AUTH0_CLIENT_SECRET ,
42
37
AUTH0_AUDIENCE ,
43
38
AUTH0_COOKIE_NAME ,
39
+ public : {
40
+ AUTH0_COOKIE_NAME
41
+ }
44
42
} ,
45
43
build : {
46
- transpile : [
47
- 'primevue'
48
- ] ,
44
+ transpile : [ "primevue" ]
49
45
} ,
50
46
// css
51
47
css : [
@@ -56,6 +52,6 @@ export default defineNuxtConfig({
56
52
] ,
57
53
components : {
58
54
global : true ,
59
- dirs : [ ' ~/components' ]
60
- } ,
55
+ dirs : [ " ~/components" ]
56
+ }
61
57
} ) ;
Original file line number Diff line number Diff line change 2
2
"private" : true ,
3
3
"scripts" : {
4
4
"dev" : " nuxi dev" ,
5
- "build" : " nuxi build" ,
5
+ "build" : " nuxt build" ,
6
+ "preview" : " nuxt preview" ,
6
7
"start" : " node .output/server/index.mjs"
7
8
},
8
9
"dependencies" : {
Original file line number Diff line number Diff line change 1
1
<template >
2
- <div >Profile</div >
2
+ <Card >
3
+ <template #title > Profile </template >
4
+ <template #content >
5
+ {{ user }}
6
+ </template >
7
+ </Card >
3
8
</template >
9
+
10
+ <script lang="ts" setup>
11
+ const user = useUser ();
12
+ </script >
Original file line number Diff line number Diff line change
1
+ import { defineNuxtPlugin } from '#app'
2
+
1
3
export default defineNuxtPlugin ( ( nuxt ) => {
2
- const session = nuxt . ssrContext . req ?. session ;
4
+ const session = nuxt . ssrContext . event . context ?. session ;
3
5
if ( session && session . user ) {
4
6
useState ( "user" , ( ) => session . user ) ;
5
7
}
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import * as Iron from '@hapi/iron'
4
4
// import { createRemoteJWKSet, jwtVerify } from 'jose'
5
5
import * as jose from 'jose'
6
6
7
- export default async ( req , res ) => {
7
+ export default defineEventHandler ( async ( event ) => {
8
8
const {
9
9
AUTH0_BASE_URL ,
10
10
AUTH0_ISSUER_BASE_URL ,
@@ -13,7 +13,7 @@ export default async (req, res) => {
13
13
AUTH0_COOKIE_NAME ,
14
14
} = process . env ;
15
15
16
- const query = parse ( req . url , true ) . query
16
+ const query = parse ( event . req . url , true ) . query
17
17
18
18
if ( query ?. error || ! query . code ) {
19
19
throw new Error ( String ( query . message ) )
@@ -61,9 +61,9 @@ export default async (req, res) => {
61
61
const date = new Date ( ) ;
62
62
date . setDate ( date . getDate ( ) + 1 ) ;
63
63
64
- res . writeHead ( 302 , {
64
+ event . res . writeHead ( 302 , {
65
65
"Set-cookie" : `${ AUTH0_COOKIE_NAME } =${ sealedCookie } ; Path=/; Secure; SameSite=Lax; Expires=${ date . toUTCString ( ) } ` ,
66
66
Location : "/" ,
67
67
} ) ;
68
- res . end ( ) ;
69
- } ;
68
+ event . res . end ( ) ;
69
+ } ) ;
Original file line number Diff line number Diff line change 1
- export default async ( req , res ) => {
1
+ export default defineEventHandler ( async ( event ) => {
2
2
const {
3
3
AUTH0_BASE_URL ,
4
4
AUTH0_ISSUER_BASE_URL ,
5
5
AUTH0_CLIENT_ID ,
6
6
AUTH0_AUDIENCE ,
7
7
} = process . env ;
8
-
9
8
const loginUrl = `${ AUTH0_ISSUER_BASE_URL } /authorize?response_type=code&client_id=${ AUTH0_CLIENT_ID } &redirect_uri=${ AUTH0_BASE_URL } /api/auth/callback&scope=openid%20profile%20email&audience=${ AUTH0_AUDIENCE } ` ;
10
-
11
- res . writeHead ( 302 , {
9
+ event . res . writeHead ( 302 , {
12
10
Location : loginUrl ,
13
11
} ) ;
14
- res . end ( ) ;
15
- } ;
12
+ event . res . end ( ) ;
13
+ } )
Original file line number Diff line number Diff line change 1
- export default async ( req , res ) => {
1
+ export default defineEventHandler ( async ( event ) => {
2
2
const { AUTH0_ISSUER_BASE_URL , AUTH0_CLIENT_ID , AUTH0_COOKIE_NAME } = process . env ;
3
-
4
- res . writeHead ( 302 , {
5
- "Set-cookie" : `${ AUTH0_COOKIE_NAME } =; Path=/; Secure; SameSite=Lax; Max-Age=0` ,
3
+ event . res . writeHead ( 302 , {
4
+ "Set-cookie" : `${ AUTH0_COOKIE_NAME } =; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=0` ,
6
5
Location : `${ AUTH0_ISSUER_BASE_URL } /v2/logout?client_id=${ AUTH0_CLIENT_ID } ` ,
7
6
} ) ;
8
- res . end ( ) ;
9
- } ;
7
+ event . res . end ( ) ;
8
+ } )
Original file line number Diff line number Diff line change 1
- export default async ( req , res ) => {
2
- const data = await fetch ( `${ process . env . AUTH0_ISSUER_BASE_URL } /userinfo` , {
3
- headers : {
4
- Authorization : `Bearer ${ req . session . access_token } ` ,
5
- } ,
6
- } ) ;
7
- return await data . json ( ) ;
8
- } ;
1
+ import { getCookie } from 'h3'
2
+ import Iron from '@hapi/iron' ;
3
+
4
+ const { AUTH0_COOKIE_NAME , AUTH0_CLIENT_SECRET } = process . env
5
+
6
+ export default defineEventHandler ( async ( event ) => {
7
+ try {
8
+ const sealed = getCookie ( event , AUTH0_COOKIE_NAME )
9
+ if ( sealed ) {
10
+ const unsealed = await Iron . unseal ( sealed , AUTH0_CLIENT_SECRET , Iron . defaults ) ;
11
+ return unsealed . user
12
+
13
+ // console.log(unsealed.access_token)
14
+ // const data = await fetch(`${process.env.AUTH0_ISSUER_BASE_URL}/userinfo`, {
15
+ // headers: {
16
+ // Authorization: `Bearer ${unsealed.access_token}`,
17
+ // },
18
+ // });
19
+ // return await data.json();
20
+ }
21
+ } catch ( err ) {
22
+ console . log ( err . message ) ;
23
+ }
24
+ } )
Original file line number Diff line number Diff line change
1
+ import { getCookie } from 'h3'
2
+ export default defineEventHandler ( ( event ) => {
3
+ const { AUTH0_COOKIE_NAME } = process . env
4
+ const token = getCookie ( event , AUTH0_COOKIE_NAME )
5
+ if ( event . req . url . includes ( '/api/auth/' ) ) {
6
+ return
7
+ }
8
+ if ( event . req . url . includes ( '/api/' ) ) {
9
+ if ( Boolean ( token ) !== true ) {
10
+ event . res . statusCode = 401
11
+ event . res . end ( 'You must be signed in to access to resource.' )
12
+ }
13
+ } else {
14
+ // if (Boolean(token) !== true) {
15
+ // event.res.writeHead(302, {
16
+ // "Set-cookie": `${AUTH0_COOKIE_NAME}=; Path=/; Secure; SameSite=Lax; Expires=`,
17
+ // Location: "/login",
18
+ // });
19
+ // event.res.end();
20
+ // }
21
+ }
22
+ } )
Original file line number Diff line number Diff line change 1
1
import Iron from "@hapi/iron" ;
2
2
import { getCookie } from 'h3'
3
3
4
- export default async ( req , res ) => {
4
+ export default defineEventHandler ( async ( event ) => {
5
5
const { AUTH0_COOKIE_NAME , AUTH0_CLIENT_SECRET } = process . env ;
6
- const cookie = getCookie ( req , AUTH0_COOKIE_NAME )
6
+ const cookie = getCookie ( event . req , AUTH0_COOKIE_NAME )
7
7
8
8
if ( cookie != null ) {
9
9
const session = await Iron . unseal (
10
10
cookie ,
11
11
AUTH0_CLIENT_SECRET ,
12
12
Iron . defaults
13
13
) ;
14
- req . session = session ;
14
+ event . context . session = session ;
15
15
}
16
- } ;
16
+ } ) ;
You can’t perform that action at this time.
0 commit comments