Skip to content
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

Adds loginHandler to dbAuth for custom login checks #3111

Merged
merged 9 commits into from
Jul 30, 2021
Merged

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Jul 23, 2021

This adds two feature requests to dbAuth:

  1. Ability to deny login to a user that would otherwise be able to log in (username and password match). A great example of this is a user that hasn't verified their email address yet.
  2. Ability to skip auto-login after successful signup. Now you can return a message to the user instead, something like "Verify your email to finish signup".

1. New loginHandler()

By throwing an error with a message in the new loginHandler() option, you can deny them access and show a message:

image

2. signupHandler() Updates

If you return a user from this function then that user will be signed in.

If you return a string, they will NOT be logged in, and {message: "My text here"} will be returned when you call the signUp() function that you get from useAuth().

If you throw an error, {error: "Error message"} will be returned from signUp().

Breaking Changes

api/src/functions/auth.js

This will require that users who have implemented dbAuth since the last release add a loginHandler() function in their api/src/functions/auth.js setup.

A new instance of DbAuthHandler is created and passed several options. loginHandler() needs to be added to that list:

  const authHandler = new DbAuthHandler(event, context, {
    db: db,
    authModelAccessor: 'user',
    authFields: {
      id: 'id',
      username: 'email',
      hashedPassword: 'hashedPassword',
      salt: 'salt',
    },
    signupHandler: ({ username, hashedPassword, salt, userAttributes }) => {
      return db.user.create({
        data: {
          email: username,
          hashedPassword: hashedPassword,
          salt: salt,
          // name: userAttributes.name
        },
      })
    },
    loginExpires: 60 * 60 * 24 * 365 * 10,

    // ********* ADD THIS FUNCTION ************
    loginHandler: (user) => {
      return user
    },
  })

No changes are necessary to signupHandler() as this is NEW functionality and existing functionality (returning a user or throw an error) is preserved.

Login/Signup Components

The response from signUp() is now an object with an error key if there was an error, or a message key if signupHandler() returns a string. Previously, error messages were returned with a message key only.

If a user is returned and logged in, an object is returned with an id key containing the id of the user.

Docs

Generated functions and components have updated comments explaining what to return in these function in various circumstances. redwoodjs.com doc updates are coming that explain these same changes.

Closes #3064

@cannikin cannikin self-assigned this Jul 23, 2021
@cannikin cannikin added release:breaking This PR is a breaking change topic/auth labels Jul 23, 2021
@thedavidprice thedavidprice added this to the next-release-priority milestone Jul 28, 2021
@cannikin cannikin merged commit f7ccf50 into main Jul 30, 2021
@cannikin cannikin deleted the rc-login-handler branch July 30, 2021 22:47
dac09 added a commit to dac09/redwood that referenced this pull request Aug 2, 2021
…om-functions-test

* 'main' of github.com:redwoodjs/redwood:
  Purged cached Magic.link token at logout (redwoodjs#2862)
  Prerender non-index pages (redwoodjs#3113)
  Adds loginHandler to dbAuth for custom login checks (redwoodjs#3111)
  Adds async/await to beforeResolver rule function invocation (redwoodjs#3072)
  Supabase to support Twitch auth (redwoodjs#3134)
  fix(project-generator): Add RW_PATH to execa options (redwoodjs#3143)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:breaking This PR is a breaking change topic/auth
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dbAuth: Add a loginHandler similar to the signupHandler
2 participants