I implemented signup flow and i am getting temporary password for login after login i am getting session token and set user password with session token but after setting the password. it show status confirmed but email: not verified
Below is the Signup, login and setpassword after first login>
Signup
import { CognitoIdentityProviderClient, AdminCreateUserCommand } from '@aws-sdk/client-cognito-identity-provider';
const config = {
region: 'ap-southeast-2', // Replace with your desired region
credentials: {
accessKeyId: 'accesKey', // Replace with your AWS Access Key ID
secretAccessKey: 'SecrectAccessKey', // Replace with your AWS Secret Access Key
},
};
export async function registerUser(request) {
if (request.method === 'POST') {
const requestBody = await request.text();
const body = JSON.parse(requestBody);
const { email, password, clientId, userPoolId } = body;
const client = new CognitoIdentityProviderClient(config);
const input = {
UserPoolId: userPoolId,
Username: email,
// Password: password, // Temporary password for first-time login
UserAttributes: [
{
Name: 'email',
Value: email,
},
],
// Add more properties as needed
};
const command = new AdminCreateUserCommand(input);
try {
const response = await client.send(command);
console.log(response);
console.log('User created successfully:', response);
return new Response(JSON.stringify({ message: 'User created successfully', user: response }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error('Error creating user:', error);
return new Response(JSON.stringify({ error: 'User creation failed' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
} else {
return new Response('Invalid request method', { status: 405 });
}
}
Login After signup
import { CognitoIdentityProviderClient, AdminInitiateAuthCommand } from '@aws-sdk/client-cognito-identity-provider';
const config = {
region: 'ap-southeast-2', // Replace with your desired region
credentials: {
accessKeyId: 'accesKey', // Replace with your AWS Access Key ID
secretAccessKey: 'SecrectAccessKey', // Replace with your AWS Secret Access Key
},
};
export async function loginUser(request) {
if (request.method === 'POST') {
const requestBody = await request.text();
const body = JSON.parse(requestBody);
const { email, password, clientId, userPoolId } = body;
const client = new CognitoIdentityProviderClient(config);
const authParams = {
AuthFlow: 'ADMIN_USER_PASSWORD_AUTH', // Use this for admin-initiated authentication
ClientId: clientId,
UserPoolId: userPoolId,
AuthParameters: {
USERNAME: email,
PASSWORD: password,
},
};
const authCommand = new AdminInitiateAuthCommand(authParams);
try {
const authResponse = await client.send(authCommand);
// Extract the authentication token from the response and return it
const authToken = authResponse.AuthenticationResult;
return new Response(JSON.stringify({ message: 'Login successful', token: authResponse }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error('Login error:', error);
return new Response(JSON.stringify({ error: 'Login failed', object: error }), {
status: 401,
headers: { 'Content-Type': 'application/json' },
});
}
} else {
return new Response('Invalid request method', { status: 405 });
}
}
Reset Password after first login (Force change password)
import { CognitoIdentityProviderClient, ConfirmForgotPasswordCommand } from '@aws-sdk/client-cognito-identity-provider';
const config = {
region: 'ap-southeast-2', // Replace with your desired region
credentials: {
accessKeyId: 'accesKey', // Replace with your AWS Access Key ID
secretAccessKey: 'SecrectAccessKey', // Replace with your AWS Secret Access Key
},
};
const client = new CognitoIdentityProviderClient(config);
export const setPassword = async (req) => {
if (req.method === 'POST') {
const requestBody = await request.text();
const body = JSON.parse(requestBody);
const { clientId, email, otp, newPassword } = body;
try {
const confirmForgotPasswordParams = {
ClientId: clientId,
ConfirmationCode: otp,
Password: newPassword,
Username: email,
};
const confirmForgotPasswordCommand = new ConfirmForgotPasswordCommand(confirmForgotPasswordParams);
const result = await client.send(confirmForgotPasswordCommand);
return new Response(JSON.stringify({ message: 'password updated successfull', result }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response(JSON.stringify({ error: 'password updation failed', object: error }), {
status: 401,
headers: { 'Content-Type': 'application/json' },
});
}
} else {
return new Response('Invalid request method', { status: 405 });
}
};
I After setting password email is still not verified check image below.
Basically my need is to signup user and when setting password i need to verify user email also. but not able to do so.