I want to redirect to a specific url after the user confirmation in amazon cognito.
When a user sign up he will get confirmation mail with a verification link as follows https://<>.auth.us-west-2.amazoncognito.com/confirmUser?client_id=<<>>&user_name=<<>>&confirmation_code=<<>>
If the user clicks the above link it will redirect to confirmation page.
Once the user confirmation is completed the page should redirect to my application.
Please give me some idea to solve this problem.
A callback URL indicates where the user will be redirected after a successful sign-in. Enter Sign out URL(s). A sign-out URL indicates where your user will be redirected after signing out. Select Authorization code grant to return an authorization code that is then exchanged for user pool tokens.
In order to change a Cognito user's status from FORCE_CHANGE_PASSWORD to CONFIRMED , we have to change their password. To change a Cognito user's password, use the admin-set-password command, setting the --permanent parameter. Copied!
The Amazon Cognito service receives the sign-up request from the app. After verifying that the request contains all attributes required for sign-up, the service completes the sign-up process and sends a confirmation code to the user's phone (in an SMS message) or email. The code is valid for 24 hours.
Go to AWS Cognito service and click “Manage Identity Pools”. 2. Enter “Identity pool name”, expand the “Authentication providers” section and select “Cognito” tab. This is where the Cognito authentication provider will be registered with the Identity pool.
Currently, this redirection can't be done using verification link in email. I tried adding redirect_uri to the verification URL a while back but they do not work.
Workaround
These values are passed to backend lambda which makes a GET request to https://your_domain.auth.us-west-2.amazoncognito.com/confirmUser?client_id=somevalue&user_name=some_user&confirmation_code=some_code
On success, return 302 https://myapp.com from your API Gateway
I know this is a convoluted workaround for such a simple requirement. The best way would be to raise a feature request and hope they support a redirect_uri in the Cognito URL.
EDIT
To save your lambda costs, you could also use an HTTP endpoint in your API and make a request to the cognito service endpoint for your region. Example:
POST HTTP/1.1 Host: cognito-idp.us-east-1.amazonaws.com x-amz-target: AWSCognitoIdentityProviderService.ConfirmSignUp Content-Type: application/x-amz-json-1.1 { "ClientId":"xxxxxxxxxxxxx", "ConfirmationCode":"123456", "Username":"username" }
I got this to work with the help of above answer from @agent420 and examining the github issue https://github.com/aws-amplify/amplify-js/issues/612
So here is the complete process that I followed.
exports.handler = (event, context, callback) => { // Identify why was this function invoked if(event.triggerSource === "CustomMessage_SignUp") { console.log('function triggered'); console.log(event); // Ensure that your message contains event.request.codeParameter. This is the placeholder for code that will be sent const { codeParameter } = event.request const { userName, region } = event const { clientId } = event.callerContext const { email } = event.request.userAttributes const url = 'https://example.com/api/dev/user/confirm' const link = `<a href="${url}?code=${codeParameter}&username=${userName}&clientId=${clientId}®ion=${region}&email=${email}" target="_blank">here</a>` event.response.emailSubject = "Your verification link"; // event.request.codeParameter event.response.emailMessage = `Thank you for signing up. Click ${link} to verify your email.`; } // Return to Amazon Cognito callback(null, event); };
Your email will be sent with the subject and message specified in event.response.emailSubject
and event.response.emailMessage
. The user will directed to the url specified in the url
variable.
I used a lambda for this with the use of AWS APIGateway. Given below is the code I wrote in nodejs where I used a 301 redirect.
'use strict'; var AWS = require('aws-sdk'); AWS.config.setPromisesDependency(require('bluebird')); var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: process.env.REGION }); module.exports.verifyEmailAddress = (req, context, callback) => { console.log('req'); console.log(req); const confirmationCode = req.queryStringParameters.code const username = req.queryStringParameters.username const clientId = req.queryStringParameters.clientId const region = req.queryStringParameters.region const email = req.queryStringParameters.email let params = { ClientId: clientId, ConfirmationCode: confirmationCode, Username: username } var confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise() confirmSignUp.then( (data) => { let redirectUrl = process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL; const response = { statusCode: 301, headers: { Location: redirectUrl, } }; return callback(null, response); } ).catch( (error) => { callback(error) } ) }
Replace environmental variables REGION
and POST_REGISTRATION_VERIFICATION_REDIRECT_URL
with the values of yours according to the requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With