11import AdminForth , { AdminForthPlugin , Filters , suggestIfTypo , AdminForthDataTypes } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthComponentDeclaration , AdminForthResourceColumn , AdminForthResource , BeforeLoginConfirmationFunction , HttpExtra } from "adminforth" ;
33import type { PluginOptions } from './types.js' ;
4+ import { error } from "console" ;
45
56
67export default class OpenSignupPlugin extends AdminForthPlugin {
@@ -81,11 +82,6 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
8182 meta : {
8283 customLayout : true ,
8384 pluginInstanceId : this . pluginInstanceId ,
84- passwordField : {
85- minLength : passwordField . minLength ,
86- maxLength : passwordField . maxLength ,
87- validation : passwordField . validation
88- } ,
8985 requestEmailConfirmation : ! ! this . options . confirmEmails
9086 }
9187 }
@@ -146,27 +142,43 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
146142
147143
148144 setupEndpoints ( server : IHttpServer ) {
145+
146+ server . endpoint ( {
147+ method : 'GET' ,
148+ path : `/plugin/${ this . pluginInstanceId } /password-constraints` ,
149+ noAuth : true ,
150+ handler : async ( { tr} ) => {
151+ return {
152+ minLength : this . passwordField . minLength ,
153+ maxLength : this . passwordField . maxLength ,
154+ validation : await Promise . all (
155+ this . passwordField . validation . map ( async ( { regExp, message } ) => ( { regExp, message : await tr ( message , 'opensignup' ) } ) )
156+ ) ,
157+ } ;
158+ }
159+ } ) ;
160+
149161 server . endpoint ( {
150162 method : 'POST' ,
151163 path : `/plugin/${ this . pluginInstanceId } /complete-verified-signup` ,
152164 noAuth : true ,
153- handler : async ( { body, response, headers, query, cookies } ) => {
165+ handler : async ( { body, response, headers, query, cookies, tr } ) => {
154166 const { token, password } = body ;
155167 const { email } = await this . adminforth . auth . verify ( token , 'tempVerifyEmailToken' , false ) ;
156168 if ( ! email ) {
157169 return { error : 'Invalid token' , ok : false } ;
158170 }
159171
160172 if ( ! password ) {
161- return { error : 'Password is required' , ok : false } ;
173+ return { error : tr ( 'Password is required' , 'opensignup' ) , ok : false } ;
162174 }
163175 const userRecord = await this . adminforth . resource ( this . authResource . resourceId ) . get ( Filters . EQ ( this . emailField . name , email ) ) ;
164176 if ( ! userRecord ) {
165- return { error : 'User not found' , ok : false } ;
177+ return { error : tr ( 'User not found' ) , ok : false } ;
166178 }
167179
168180 if ( userRecord [ this . options . confirmEmails . emailConfirmedField ] ) {
169- return { error : 'Email already confirmed' , ok : false } ;
181+ return { error : tr ( 'Email already confirmed' ) , ok : false } ;
170182 }
171183
172184 await this . adminforth . resource ( this . authResource . resourceId ) . update ( userRecord [ this . authResource . columns . find ( ( col ) => col . primaryKey ) . name ] , {
@@ -181,7 +193,7 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
181193 method : 'POST' ,
182194 path : `/plugin/${ this . pluginInstanceId } /signup` ,
183195 noAuth : true ,
184- handler : async ( { body, response, headers, query, cookies } ) => {
196+ handler : async ( { body, response, headers, query, cookies, tr } ) => {
185197 const { email, url, password } = body ;
186198
187199 // validate email
@@ -196,15 +208,21 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
196208 // validate password
197209 if ( ! this . options . confirmEmails ) {
198210 if ( password . length < this . passwordField . minLength ) {
199- return { error : `Password must be at least ${ this . passwordField . minLength } characters long` , ok : false } ;
211+ return {
212+ error : tr ( `Password must be at least ${ this . passwordField . minLength } characters long` , 'opensignup' ) ,
213+ ok : false
214+ } ;
200215 }
201216 if ( password . length > this . passwordField . maxLength ) {
202- return { error : `Password must be at most ${ this . passwordField . maxLength } characters long` , ok : false } ;
217+ return {
218+ error : tr ( `Password must be at most ${ this . passwordField . maxLength } characters long` , 'opensignup' ) ,
219+ ok : false
220+ } ;
203221 }
204222 if ( this . passwordField . validation ) {
205223 for ( const { regExp, message } of this . passwordField . validation ) {
206224 if ( ! new RegExp ( regExp ) . test ( password ) ) {
207- return { error : message , ok : false } ;
225+ return { error : tr ( message , 'opensignup' ) , ok : false } ;
208226 }
209227 }
210228 }
@@ -213,7 +231,7 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
213231 // first check again if email already exists
214232 const existingUser = await this . adminforth . resource ( this . authResource . resourceId ) . get ( Filters . EQ ( this . emailField . name , email ) ) ;
215233 if ( ( ! this . options . confirmEmails && existingUser ) || ( this . options . confirmEmails && existingUser ?. [ this . emailConfirmedField . name ] ) ) {
216- return { error : 'Email already exists' , ok : false } ;
234+ return { error : tr ( 'Email already exists' , 'opensignup' ) , ok : false } ;
217235 }
218236
219237 // create user
@@ -237,40 +255,40 @@ export default class OpenSignupPlugin extends AdminForthPlugin {
237255
238256 const verifyToken = this . adminforth . auth . issueJWT ( { email, issuer : brandName } , 'tempVerifyEmailToken' , '2h' ) ;
239257 process . env . HEAVY_DEBUG && console . log ( '🐛Sending reset tok to' , verifyToken ) ;
240- const emailText = `
258+ const emailText = tr ( `
241259 Dear user,
242- Welcome to $ {brandName } !
260+ Welcome to {brandName}!
243261
244262 To confirm your email, click the link below:\n\n
245263
246- $ {url } ?verifyToken=$ {verifyToken } \n\n
264+ {url}?verifyToken={verifyToken}\n\n
247265
248266 If you didn't request this, please ignore this email.\n\n
249267 Link is valid for 2 hours.\n\n
250268
251269 Thanks,
252- The $ {brandName } Team
270+ The {brandName} Team
253271
254- ` ;
272+ ` , 'opensignup' , { brandName, url, verifyToken }
273+ ) ;
255274
256- const emailHtml = `
257- <html>
258- <head></head>
259- <body>
260- <p>Dear user,</p>
261- <p>Welcome to ${ brandName } !</p>
262- <p>To confirm your email, click the link below:</p>
263- <a href="${ url } ?token=${ verifyToken } ">Confirm email</a>
264- <p>If you didn't request this, please ignore this email.</p>
265- <p>Link is valid for 2 hours.</p>
266- <p>Thanks,</p>
267- <p>The ${ brandName } Team</p>
268- </body>
269- </html>
270-
271-
272- ` ;
273- const emailSubject = `Signup request at ${ brandName } ` ;
275+ const emailHtml = tr ( `
276+ <html>
277+ <head></head>
278+ <body>
279+ <p>Dear user,</p>
280+ <p>Welcome to {brandName}!</p>
281+ <p>To confirm your email, click the link below:</p>
282+ <a href="{url}?token={verifyToken}">Confirm email</a>
283+ <p>If you didn't request this, please ignore this email.</p>
284+ <p>Link is valid for 2 hours.</p>
285+ <p>Thanks,</p>
286+ <p>The {brandName} Team</p>
287+ </body>
288+ </html>
289+ ` , 'opensignup' , { brandName, url, verifyToken } ) ;
290+
291+ const emailSubject = tr ( `Signup request at {brandName}` , 'opensignup' , { brandName } ) ;
274292
275293 // send email with AWS SES this.options.providerOptions.AWS_SES
276294 this . options . confirmEmails . adapter . sendEmail ( this . options . confirmEmails . sendFrom , email , emailText , emailHtml , emailSubject ) ;
0 commit comments