@@ -10,6 +10,13 @@ interface OAuthPluginOptions {
10
10
emailField : string ;
11
11
emailConfirmedField ?: string ;
12
12
adapters : OAuth2Adapter [ ] ;
13
+ iconOnly ?: boolean ;
14
+ pill ?: boolean ;
15
+ authenticationExpireDuration ?: number ;
16
+ openSignup ?: {
17
+ enabled ?: boolean ;
18
+ defaultFieldValues ?: Record < string , any > ;
19
+ } ;
13
20
}
14
21
15
22
export default class OAuthPlugin extends AdminForthPlugin {
@@ -22,7 +29,17 @@ export default class OAuthPlugin extends AdminForthPlugin {
22
29
if ( ! options . emailField ) {
23
30
throw new Error ( 'OAuthPlugin: emailField is required' ) ;
24
31
}
25
- this . options = options ;
32
+
33
+ // Set default values for openSignup
34
+ this . options = {
35
+ ...options ,
36
+ iconOnly : options . iconOnly ?? false ,
37
+ pill : options . pill ?? false ,
38
+ openSignup : {
39
+ enabled : options . openSignup ?. enabled ?? false ,
40
+ defaultFieldValues : options . openSignup ?. defaultFieldValues ?? { } ,
41
+ }
42
+ } ;
26
43
}
27
44
28
45
async modifyResourceConfig ( adminforth : IAdminForth , resource : AdminForthResource ) {
@@ -31,11 +48,6 @@ export default class OAuthPlugin extends AdminForthPlugin {
31
48
this . adminforth = adminforth ;
32
49
this . resource = resource ;
33
50
34
- // Add custom page for OAuth callback
35
- if ( ! adminforth . config . customization . customPages ) {
36
- adminforth . config . customization . customPages = [ ] ;
37
- }
38
-
39
51
adminforth . config . customization . customPages . push ( {
40
52
path : '/oauth/callback' ,
41
53
component : {
@@ -72,24 +84,30 @@ export default class OAuthPlugin extends AdminForthPlugin {
72
84
}
73
85
74
86
// Register the component with the correct plugin path
75
- const componentPath = `@@/plugins/${ this . constructor . name } /OAuthLoginButton .vue` ;
76
- this . componentPath ( 'OAuthLoginButton .vue' ) ;
87
+ const componentPath = `@@/plugins/${ this . constructor . name } /OAuthLoginButtons .vue` ;
88
+ this . componentPath ( 'OAuthLoginButtons .vue' ) ;
77
89
78
90
const baseUrl = adminforth . config . baseUrl || '' ;
79
- this . options . adapters . forEach ( adapter => {
91
+ const providers = this . options . adapters . map ( adapter => {
80
92
const state = Buffer . from ( JSON . stringify ( {
81
93
provider : adapter . constructor . name
82
94
} ) ) . toString ( 'base64' ) ;
83
95
84
- adminforth . config . customization . loginPageInjections . underInputs . push ( {
85
- file : componentPath ,
86
- meta : {
87
- authUrl : `${ adapter . getAuthUrl ( ) } &state=${ state } ` ,
88
- provider : adapter . constructor . name ,
89
- baseUrl,
90
- icon : adapter . getIcon ?.( ) || ''
91
- }
92
- } ) ;
96
+ return {
97
+ authUrl : `${ adapter . getAuthUrl ( ) } &state=${ state } ` ,
98
+ provider : adapter . constructor . name ,
99
+ baseUrl,
100
+ icon : adapter . getIcon ( ) ,
101
+ } ;
102
+ } ) ;
103
+
104
+ adminforth . config . customization . loginPageInjections . underInputs . push ( {
105
+ file : componentPath ,
106
+ meta : {
107
+ providers,
108
+ iconOnly : this . options . iconOnly ,
109
+ pill : this . options . pill ,
110
+ }
93
111
} ) ;
94
112
}
95
113
@@ -124,7 +142,7 @@ export default class OAuthPlugin extends AdminForthPlugin {
124
142
response,
125
143
username,
126
144
pk : user . id ,
127
- expireInDays : this . adminforth . config . auth . rememberMeDays
145
+ expireInDays : this . options . authenticationExpireDuration ? this . options . authenticationExpireDuration : this . adminforth . config . auth . rememberMeDays
128
146
} ) ;
129
147
}
130
148
@@ -137,7 +155,7 @@ export default class OAuthPlugin extends AdminForthPlugin {
137
155
path : '/oauth/callback' ,
138
156
noAuth : true ,
139
157
handler : async ( { query, response, headers, cookies, requestUrl } ) => {
140
- const { code, state } = query ;
158
+ const { code, state, redirect_uri } = query ;
141
159
if ( ! code ) {
142
160
return { error : 'No authorization code provided' } ;
143
161
}
@@ -155,20 +173,26 @@ export default class OAuthPlugin extends AdminForthPlugin {
155
173
return { error : 'Invalid OAuth provider' } ;
156
174
}
157
175
158
- const userInfo = await adapter . getTokenFromCode ( code ) ;
176
+ const userInfo = await adapter . getTokenFromCode ( code , redirect_uri ) ;
159
177
160
178
let user = await this . adminforth . resource ( this . resource . resourceId ) . get ( [
161
179
Filters . EQ ( this . options . emailField , userInfo . email )
162
180
] ) ;
163
181
164
182
if ( ! user ) {
183
+ // Check if open signup is enabled
184
+ if ( ! this . options . openSignup ?. enabled ) {
185
+ return {
186
+ error : 'User not found and open signup is disabled' ,
187
+ redirectTo : '/login'
188
+ } ;
189
+ }
190
+
165
191
// When creating a new user, set emailConfirmedField to true if it's configured
166
192
const createData : any = {
167
- id : randomUUID ( ) ,
168
- created_at : new Date ( ) . toISOString ( ) ,
169
193
[ this . options . emailField ] : userInfo . email ,
170
- role : 'user ' ,
171
- password_hash : ''
194
+ [ this . adminforth . config . auth . passwordHashField ] : '' ,
195
+ ... this . options . openSignup . defaultFieldValues
172
196
} ;
173
197
174
198
if ( this . options . emailConfirmedField ) {
0 commit comments