Search code examples
javascriptnode.jsexpressauthenticationsms

Token not received after OTP submission in Phone Email's phone authentication in Node JS


I am integrating Phone Email’s “Login with Phone” Button in Node JS on my website. I have created button and calling their URL, it opens a popup to enter mobile number. After submitting, it is showing OTP window. I successfully got the OTP SMS and enter the OTP but I am not getting the verified number back on my website. It is the code snippet of my webpage.

// Main endpoint
app.get('/', (req, res) => {
// Set query parameters for constructing the URL
const queryParams = {
    countryCode: process.env.COUNTRY_CODE,
    phoneNo: process.env.PHONE_NO,
    redirectUrl: process.env.REDIRECT_URL,
};

// Construct URL for the phone sign-in page
const phoneSignInUrl = `https://www.phone.email/auth/sign-in?countrycode=${queryParams.countryCode}&phone_no=${queryParams.phoneNo}&redirect_url=${queryParams.redirectUrl}`;

// Render the main view with the constructed URL
return res.render('pages/index', { phoneSignInUrl });
});

Onclick of my button above code snippet called and it returns the response in my redirect URL(http://localhost:3000/auth). My auth page code snippet given below.

app.get('/auth', (req, res) => {
try {
    // Extract token from query parameters
    const token = req.query.token;

    // Check if token exists
    if (!token) {
        throw new Error("Token is required for authentication.");
    }

    // Verify the JWT
    const decoded = jwt.verify(token, process.env.API_KEY, { algorithm: 'HS256' });

    // Render success view with decoded information
    const message = `${decoded.country_code} ${decoded.phone_no}`;
    const status = `success`;
    return res.render('pages/auth-success', { message, status, token });
} catch (error) {
    // Render error view with error message
    const status = 'failure';
    const message = error.message;
    return res.render('pages/auth-error', { status, message });
}
});

It successfully called but I am not getting the token. I have defined my required credential in .env file and they are correct.


Solution

  • Your are not receiving correct token variable from get parameter. Replace token with phtoken in your code. Try this code:

    app.get('/auth', (req, res) => {
        try {
            // Extract token from query parameters
            const token = req.query.phtoken;
        
            // Check if token exists
            if (!token) {
                throw new Error("Token is required for authentication.");
            }
        
            // Verify the JWT
            const decoded = jwt.verify(token, process.env.API_KEY, { algorithm: 'HS256' });
        
            // Render success view with decoded information
            const message = `${decoded.country_code} ${decoded.phone_no}`;
            const status = `success`;
            return res.render('pages/auth-success', { message, status, token });
        } catch (error) {
            // Render error view with error message
            const status = 'failure';
            const message = error.message;
            return res.render('pages/auth-error', { status, message });
        }
        });