11import type { Strategy , ModuleOptions , ProviderNames , SchemeNames } from './types' ;
22import type { Nuxt } from '@nuxt/schema' ;
3+ import { addAuthorize , addLocalAuthorize , assignAbsoluteEndpoints , assignDefaults } from './utils/provider' ;
34import { ProviderAliases } from './runtime/providers' ;
45import * as AUTH_PROVIDERS from './runtime/providers' ;
56import { resolvePath } from '@nuxt/kit' ;
@@ -16,6 +17,113 @@ export const BuiltinSchemes = {
1617 auth0 : 'Auth0Scheme' ,
1718} ;
1819
20+ export const OAUTH2DEFAULTS = {
21+ accessType : undefined ,
22+ redirectUri : undefined ,
23+ logoutRedirectUri : undefined ,
24+ clientId : undefined ,
25+ clientSecretTransport : 'body' ,
26+ audience : undefined ,
27+ grantType : undefined ,
28+ responseMode : undefined ,
29+ acrValues : undefined ,
30+ autoLogout : false ,
31+ endpoints : {
32+ logout : undefined ,
33+ authorization : undefined ,
34+ token : undefined ,
35+ userInfo : undefined ,
36+ } ,
37+ scope : [ ] ,
38+ token : {
39+ property : 'access_token' ,
40+ expiresProperty : 'expires_in' ,
41+ type : 'Bearer' ,
42+ name : 'Authorization' ,
43+ maxAge : false ,
44+ global : true ,
45+ prefix : '_token.' ,
46+ expirationPrefix : '_token_expiration.' ,
47+ } ,
48+ idToken : {
49+ property : 'id_token' ,
50+ maxAge : 1800 ,
51+ prefix : '_id_token.' ,
52+ expirationPrefix : '_id_token_expiration.' ,
53+ } ,
54+ refreshToken : {
55+ property : 'refresh_token' ,
56+ maxAge : 60 * 60 * 24 * 30 ,
57+ prefix : '_refresh_token.' ,
58+ expirationPrefix : '_refresh_token_expiration.' ,
59+ httpOnly : false ,
60+ } ,
61+ user : {
62+ property : false ,
63+ } ,
64+ responseType : 'token' ,
65+ codeChallengeMethod : false ,
66+ clientWindow : false ,
67+ clientWindowWidth : 400 ,
68+ clientWindowHeight : 600
69+ } ;
70+
71+ export const LOCALDEFAULTS = {
72+ cookie : {
73+ name : undefined
74+ } ,
75+ endpoints : {
76+ csrf : {
77+ url : '/api/csrf-cookie' ,
78+ } ,
79+ login : {
80+ url : '/api/auth/login' ,
81+ method : 'post' ,
82+ } ,
83+ logout : {
84+ url : '/api/auth/logout' ,
85+ method : 'post' ,
86+ } ,
87+ user : {
88+ url : '/api/auth/user' ,
89+ method : 'get' ,
90+ } ,
91+ refresh : {
92+ url : '/api/auth/refresh' ,
93+ method : 'POST' ,
94+ } ,
95+ } ,
96+ token : {
97+ expiresProperty : 'expires_in' ,
98+ property : 'token' ,
99+ type : 'Bearer' ,
100+ name : 'Authorization' ,
101+ maxAge : false ,
102+ global : true ,
103+ required : true ,
104+ prefix : '_token.' ,
105+ expirationPrefix : '_token_expiration.' ,
106+ } ,
107+ refreshToken : {
108+ property : 'refresh_token' ,
109+ data : 'refresh_token' ,
110+ maxAge : 60 * 60 * 24 * 30 ,
111+ required : true ,
112+ tokenRequired : false ,
113+ prefix : '_refresh_token.' ,
114+ expirationPrefix : '_refresh_token_expiration.' ,
115+ httpOnly : false ,
116+ } ,
117+ autoLogout : false ,
118+ user : {
119+ property : 'user' ,
120+ autoFetch : true ,
121+ } ,
122+ clientId : false ,
123+ grantType : false ,
124+ scope : false ,
125+ } ;
126+
19127export interface ImportOptions {
20128 name : string ;
21129 as : string ;
@@ -44,13 +152,15 @@ export async function resolveStrategies(nuxt: Nuxt, options: ModuleOptions) {
44152 strategy . provider = strategy . name as ProviderNames ;
45153 }
46154
155+ // Determine if SSR is enabled
156+ strategy . ssr = nuxt . options . ssr
157+
47158 // Try to resolve provider
48- const provider = await resolveProvider ( strategy . provider ) ;
159+ const provider = await resolveProvider ( strategy . provider , nuxt , strategy ) ;
49160
50161 delete strategy . provider ;
51162
52- // check that the provider isn't a nuxt module
53- if ( typeof provider === 'function' ) {
163+ if ( typeof provider === "function" ) {
54164 provider ( nuxt , strategy ) ;
55165 }
56166
@@ -103,7 +213,7 @@ export async function resolveScheme(scheme: string) {
103213 }
104214}
105215
106- export async function resolveProvider ( provider : string | ( ( ...args : any [ ] ) => any ) ) {
216+ export async function resolveProvider ( provider : string | ( ( ...args : any [ ] ) => any ) , nuxt : Nuxt , strategy : Strategy ) {
107217
108218 provider = ( ProviderAliases [ provider as keyof typeof ProviderAliases ] || provider ) ;
109219
@@ -113,11 +223,26 @@ export async function resolveProvider(provider: string | ((...args: any[]) => an
113223
114224 // return the provider
115225 if ( typeof provider === 'function' ) {
116- return provider ;
226+ return provider ( nuxt , strategy ) ;
117227 }
118228
119229 // return an empty function as it doesn't use a provider
120230 if ( typeof provider === 'string' ) {
121- return ( nuxt , strategy ) => { }
231+ return ( nuxt : Nuxt , strategy : Strategy ) => {
232+ if ( [ 'oauth2' , 'openIDConnect' , 'auth0' ] . includes ( strategy . scheme ! ) && strategy . ssr ) {
233+ assignDefaults ( strategy as any , OAUTH2DEFAULTS )
234+ addAuthorize ( nuxt , strategy as any , true )
235+ }
236+
237+ if ( [ 'refresh' , 'local' , 'cookie' ] . includes ( strategy . scheme ! ) && strategy . ssr ) {
238+ assignDefaults ( strategy as any , LOCALDEFAULTS )
239+
240+ if ( strategy . url ) {
241+ assignAbsoluteEndpoints ( strategy as any ) ;
242+ }
243+
244+ addLocalAuthorize ( nuxt , strategy as any )
245+ }
246+ }
122247 }
123248}
0 commit comments