crayden
crayden

Reputation: 2270

How to sign user in after registration using AWS Amplify and React

When a user signs up using the withAuthenticator component, they are automatically logged in after confirming their email address. It this possible to automatically sign them in using a custom sign-in flow and the Auth object? How would this be accomplished?

Upvotes: 0

Views: 1415

Answers (2)

Zog
Zog

Reputation: 1143

As of Amplify JS API v4.3.29 it is now possible. Simply include the autoSignIn attribute in the signUp method.

 Auth.signUp({
  username: 'xxxxxx',
  password: '*********',
  attributes: {
    email: 'xxxxxxxxxx'
  },
  autoSignIn: {
    enabled: true
  }
})

Upvotes: 1

Jordan The Genius
Jordan The Genius

Reputation: 56

I recently dealt with an issue in which I wanted to use the Storage module of Amplify, but had to implement the Auth module in order to do it. I had a working signin flow, and didn't want it disturbed, so I used the Auth api to sign users into AWS in the background, while signing them into my app using my original flow.

The docs describe how to sign a user in programmatically here: https://docs.amplify.aws/lib/auth/emailpassword/q/platform/js

Implementing the JS code isn't too hard

import { Auth } from 'aws-amplify';

async function signIn() {
    try {
        const user = await Auth.signIn(username, password);
    } catch (error) {
        console.log('error signing in', error);
    }
}

I implemented a pre-sign up lambda trigger to auto confirm users, their email, etc bc I didn't want this Auth flow interrupting my existing flow. This method is described here: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html#aws-lambda-triggers-pre-registration-example-2

Here are the notes I took as I was working, in case it helps to give you more context. Sorry if they're confusing, they're only really meant for me to read: https://www.evernote.com/shard/s713/sh/ccf96dcc-b51e-9963-5207-ac410b02a13a/0bcdb4bc85ea8a31b7f5621a6812c837

Upvotes: 1

Related Questions