-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: Added autosignuponlogin #9873
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
base: alpha
Are you sure you want to change the base?
Changes from 1 commit
486a405
fbf5b72
3dbb398
b8aeaa0
ebc9a64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,8 +199,23 @@ export class UsersRouter extends ClassesRouter { | |
| } | ||
|
|
||
| async handleLogIn(req) { | ||
| const user = await this._authenticateUserFromRequest(req); | ||
| const authData = req.body && req.body.authData; | ||
| let user; | ||
| let signupSessionToken; | ||
|
|
||
| try { | ||
| user = await this._authenticateUserFromRequest(req); | ||
| } catch (error) { | ||
| const autoSignupCredentials = this._prepareAutoSignupCredentials(req, error); | ||
| if (!autoSignupCredentials) { | ||
| throw error; | ||
| } | ||
| // Create the missing user but continue through the standard login path so | ||
| // that all login-time policies, triggers, and session metadata remain unchanged. | ||
| signupSessionToken = await this._autoSignupOnLogin(req, autoSignupCredentials); | ||
| user = await this._authenticateUserFromRequest(req); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Check if user has provided their required auth providers | ||
| Auth.checkIfUserHasProvidedConfiguredProvidersForLogin( | ||
| req, | ||
|
|
@@ -299,6 +314,18 @@ export class UsersRouter extends ClassesRouter { | |
|
|
||
| await createSession(); | ||
|
|
||
| if (signupSessionToken) { | ||
| // Discard the session issued by the signup shortcut; the login session we just | ||
| // created is the single source of truth for the client. | ||
| try { | ||
| await req.config.database.destroy('_Session', { sessionToken: signupSessionToken }); | ||
| } catch (sessionError) { | ||
| if (sessionError && sessionError.code !== Parse.Error.OBJECT_NOT_FOUND) { | ||
| logger.warn('Failed to clean up auto sign-up session token', sessionError); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const afterLoginUser = Parse.User.fromJSON(Object.assign({ className: '_User' }, user)); | ||
| await maybeRunTrigger( | ||
| TriggerTypes.afterLogin, | ||
|
|
@@ -317,6 +344,83 @@ export class UsersRouter extends ClassesRouter { | |
| return { response: user }; | ||
| } | ||
|
|
||
|
|
||
| _getLoginPayload(req) { | ||
| let source = req.body || {}; | ||
| if ( | ||
| (!source.username && req.query && req.query.username) || | ||
| (!source.email && req.query && req.query.email) | ||
| ) { | ||
| source = req.query; | ||
| } | ||
| return { | ||
| username: source.username, | ||
| email: source.email, | ||
| password: source.password, | ||
| }; | ||
| } | ||
|
|
||
| // Returns data for auto-signup if autoSignupOnLogin is true and the error is that the user doesn't exist. | ||
| // If the conditions don't match, we return `null`. | ||
| // This gathers minimal credentials so that the signup path can rely on RestWrite's own validation. | ||
| _prepareAutoSignupCredentials(req, error) { | ||
| if (!req.config.autoSignupOnLogin) { | ||
| return null; | ||
| } | ||
| if (!(error instanceof Parse.Error) || error.code !== Parse.Error.OBJECT_NOT_FOUND) { | ||
| return null; | ||
| } | ||
| if (req.body && req.body.authData) { | ||
| return null; | ||
| } | ||
| const payload = this._getLoginPayload(req); | ||
| const rawUsername = typeof payload.username === 'string' ? payload.username.trim() : ''; | ||
| const rawEmail = typeof payload.email === 'string' ? payload.email.trim() : ''; | ||
| const password = payload.password; | ||
| const hasUsername = rawUsername.length > 0; | ||
| const hasEmail = rawEmail.length > 0; | ||
| if (!hasUsername && !hasEmail) { | ||
| return null; | ||
| } | ||
| if (typeof password !== 'string') { | ||
| return null; | ||
| } | ||
| return { | ||
| username: hasUsername ? rawUsername : rawEmail, | ||
| email: hasEmail ? rawEmail : undefined, | ||
| password, | ||
|
Comment on lines
+407
to
+410
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider validation implications of username fallback to email. When only an email is provided (line 408), the username is set to the email address. This could cause issues:
Verify that this approach is validated correctly: #!/bin/bash
# Check username validation rules that might conflict with email format
rg -n -C3 'username.*validation|validateUsername' --type=jsConsider requiring an explicit username for auto-signup, or document this behavior clearly in the option description. 🤖 Prompt for AI Agents |
||
| }; | ||
| } | ||
|
|
||
| async _autoSignupOnLogin(req, credentials) { | ||
| const userData = { | ||
| username: credentials.username, | ||
| password: credentials.password, | ||
| }; | ||
| if (credentials.email !== undefined) { | ||
| userData.email = credentials.email; | ||
| } | ||
| // Just call the existing user creation flow so we get all schema checks, triggers, | ||
| // adapters, and side effects exactly once. | ||
| // As for params validation, RestWrite's validateAuthData will handle the validation of the params internally anyway. | ||
| const result = await rest.create( | ||
| req.config, | ||
| req.auth, | ||
| '_User', | ||
| userData, | ||
| req.info.clientSDK, | ||
| req.info.context | ||
| ); | ||
| const user = result?.response; | ||
| if (!user) { | ||
| throw new Parse.Error( | ||
| Parse.Error.INTERNAL_SERVER_ERROR, | ||
| 'Unable to automatically sign up user.' | ||
| ); | ||
| } | ||
| return user.sessionToken; | ||
| } | ||
|
|
||
| /** | ||
| * This allows master-key clients to create user sessions without access to | ||
| * user credentials. This enables systems that can authenticate access another | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.