EN-99: feat, return JWT after confirmation of account and populate company
This commit is contained in:
@@ -43,18 +43,14 @@ const confirm_password_recover_schema = {
|
||||
required : [ 'email', 'password', 'otp', 'checksum' ]//Different requirements
|
||||
};
|
||||
|
||||
const AuthorizeJWT = async(req, res) => {
|
||||
try{
|
||||
if( validator.validate( req.body , login_account_schema ).valid ){
|
||||
const { email, password } = req.body;
|
||||
async function AuthorizeJWT_email_pwd( email , password ){
|
||||
const user = await login( email, password );
|
||||
if( !user ){
|
||||
return res.status(401).send( { error : "Invalid credentials" } );
|
||||
return null;
|
||||
}
|
||||
const current_date = new Date();
|
||||
const iat = Math.floor( (current_date.getTime())/1000 );
|
||||
const renewal_exp = ( iat + 3600*jwtRenewalTimeout ) * 1000;
|
||||
|
||||
/**
|
||||
* Renew session token on every login event.
|
||||
* Previous session token is lost
|
||||
@@ -64,7 +60,6 @@ const AuthorizeJWT = async(req, res) => {
|
||||
user.session_token = session_token;
|
||||
user.session_token_exp = session_token_exp;
|
||||
await user.save();
|
||||
|
||||
const payload = {
|
||||
iat: iat,
|
||||
exp: iat + jwtTimeout * 3600,
|
||||
@@ -73,13 +68,24 @@ const AuthorizeJWT = async(req, res) => {
|
||||
sub: user.id,
|
||||
};
|
||||
const jwt = jsonwebtoken.sign( payload , jwtSecret );
|
||||
return res.status(200).send( {
|
||||
return {
|
||||
accessToken : jwt,
|
||||
payload : payload,
|
||||
session_token,
|
||||
session_token_exp,
|
||||
user : user
|
||||
} );
|
||||
};
|
||||
}
|
||||
|
||||
const AuthorizeJWT = async(req, res) => {
|
||||
try{
|
||||
if( validator.validate( req.body , login_account_schema ).valid ){
|
||||
const { email, password } = req.body;
|
||||
const retVal = await AuthorizeJWT_email_pwd( email , password );
|
||||
if( !retVal ){
|
||||
return res.status(401).send( { error : "Invalid credentials" } );
|
||||
}
|
||||
return res.send( retVal );
|
||||
}else{
|
||||
return res.status(400).send( { error : "Invalid request" } );
|
||||
}
|
||||
@@ -185,10 +191,10 @@ const ConfirmAccount = async(req, res) => {
|
||||
const content = { user_name : email };
|
||||
const receiver = email;
|
||||
await emailEvent( EMAIL_EVENTS.ACCOUNT_CONFIRMED , receiver , content );
|
||||
console.log(
|
||||
content
|
||||
);
|
||||
return res.status(200).send( { msg : "User created successfully!" } );
|
||||
|
||||
const retVal = await AuthorizeJWT_email_pwd( email , password );
|
||||
|
||||
return res.send( retVal );
|
||||
}else{
|
||||
return res.status(400).send( { error : "Invalid request" } );
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ async function login( email , password ){
|
||||
let safe_password = toSha256( password + pwd_secret );
|
||||
const user = await UserModel.findOne({
|
||||
email , password : safe_password
|
||||
},{ password : 0 , session_token : 0 , session_token_exp : 0 });
|
||||
},{ password : 0 , session_token : 0 , session_token_exp : 0 }).populate('company');
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user