Forgot password - Cognito Hosted UI and OIDC Endpoint Reference
Tags: cognito
Published: 2023-02-21 12:20:52

I am trying to do my customised UI for Cognito Login and Forgot Password using this [Stackoverflow Answer][1]. I am able to do Login request and redirect. Also I am able to get the Reset Password code to the email. But   Confirm password submit request is not working. Also I am not getting any proper error messages from the API End point. I will provide here Login API request and Confirm Password Request. 

Login Submit Request:

    exports.postLogin = async (req, res, next) => {
        const CLIENT_ID = req.query.client_id;
        const RESPONSE_TYPE = 'token';
        const REDIRECT_URI = encodeURIComponent(req.query.redirect_uri);
        const SCOPE = req.query.scope;
        const AUTH_DOMAIN = "<domain>.<region>.amazoncognito.com";
        const USERNAME = req.body.username;
        const PASSWORD = req.body.password
        const state = req.query.state;
        const XSRFTOKEN = req.body._csrf
    
        let form = {
            '_csrf': XSRFTOKEN,
            'username': `${USERNAME.toLowerCase()}`,
            'password': `${PASSWORD}`,
        }
    
        const formData = querystring.stringify(form);
        const contentLength = formData.length;
    
        console.log(formData)
    
        // Post CSRF Token and username/password to /login endpoint
        const codeRequestUrl = `https://${AUTH_DOMAIN}/login?response_type=${RESPONSE_TYPE}&client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&state=${state}`;
        console.log(codeRequestUrl)
        request({
            headers: {
                'Content-Length': contentLength,
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie': `XSRF-TOKEN=${XSRFTOKEN}; Path=/; Secure; HttpOnly; SameSite=Lax`,
            },
            uri: codeRequestUrl,
            body: formData,
            method: 'POST'
        }, (err, response, body) => {
            console.log(body)
            console.log(err);
            console.log(response.headers.location);
            console.log(response.statusCode)
            res.redirect(response.headers.location);
        });
    };


The above API is working fine. But if the user credential is wrong, the API doesn't providing proper error message.

Confirm Password Submit Request:

    exports.postConfirmForgotPassword = async (req, res, next) => {
        const req_parsed = new Url(req.url);
    
        const CLIENT_ID = req.query.client_id;
        const RESPONSE_TYPE = 'token';
        const REDIRECT_URI = encodeURIComponent(req.query.redirect_uri);
        const AUTH_DOMAIN = "<domain>.<region>.amazoncognito.com";
        const USERNAME = req.body.username;
        const ConfirmationCode = req.body.code;
        const Password = req.body.password;
        const state = req.query.state;
        const XSRFTOKEN = req.body._csrf
    
        let form = {
            '_csrf': XSRFTOKEN,
            'confirmation-code': `${ConfirmationCode}`, // I tried it with sending as 'code', 'confirmationcode'. But nothing is working.
            'password': `${Password}`,
            'username': `${USERNAME}`
        }
    
        const formData = querystring.stringify(form);
        const contentLength = formData.length;
    
        console.log(formData)
    
        // Post CSRF Token and username/password to /login endpoint
        const codeRequestUrl = `https://${AUTH_DOMAIN}/confirmForgotPassword?response_type=${RESPONSE_TYPE}&client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&state=${state}`;
        console.log(codeRequestUrl)
        request({
            headers: {
                'Content-Length': contentLength,
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie': `XSRF-TOKEN=${XSRFTOKEN}; Path=/; Secure; HttpOnly; SameSite=Lax`,
            },
            uri: codeRequestUrl,
            body: formData,
            method: 'POST'
        }, (err, response, body) => {
            console.log(err);
            console.log(response.headers.location);
            console.log(response.statusCode)
            console.log(`/login${req_parsed.query}`)
            res.redirect(`/login${req_parsed.query}`);
        });
    };

I tried it to send the request body as below too. But nothing is working. Also not seeing any documentation for this in Cognito Side.

    let form = {
        '_csrf': XSRFTOKEN,
        'code': `${ConfirmationCode}`, 
        'password': `${Password}`,
        'username': `${USERNAME}`
    }

(or)

    let form = {
        '_csrf': XSRFTOKEN,
        'confirmationcode': `${ConfirmationCode}`, 
        'password': `${Password}`,
        'username': `${USERNAME}`
    }

Also Is there any way to identify the proper error messages for the above APIs

Also I tried to call the API according to the [AWS Cognito Documentation][2]. But I am receiving bad request

    const main = async () => {
        try {
            const AUTH_DOMAIN = "mydomain.regison.amazoncognito.com";
            const request_url = `https://${AUTH_DOMAIN}/confirmforgotPassword`;
            const payload = {
                ClientId:"CLIENT_ID",
                ConfirmationCode:"****",
                Password:"***********",
                Username:"user email id",
                SecretHash:"XSRFTOKEN"
            }
            const response = await axios.post(request_url, payload);
            console.log(response)
        } catch (e) {
            console.log(e)
        }
    
    }
    
    main()


  [1]: https://stackoverflow.com/q/49012091
  [2]: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmForgotPassword.html