Devlog - 4
Today I connected my website to firestore for database
I have implemented the saving of login information of user to user collection of firestore database
This is the implementation code:
service\userService.js :
async function saveUserLogin(userRecord) {
return db
.collection("users")
.doc(userRecord.uid)
.set(
{
displayName: userRecord.displayName || null,
email: userRecord.email,
photoURL: userRecord.photoURL || null,
lastLoginAt: new Date(userRecord.metadata.lastSignInTime)
},
{merge: true}
)
}
service\userService.js :
router.post("/sessionLogin", async (req,res) => {
const idToken = req.body.idToken;
const expiresIn = 60 * 60 * 24 * 13 * 1000;
try {
const sessionCookie = await admin.auth().createSessionCookie(idToken, { expiresIn: expiresIn });
const options = {maxAge: expiresIn, httpOnly: true, secure: false};
const decodedClaims = await admin.auth().verifySessionCookie(sessionCookie, true);
const userRecord = await admin.auth().getUser(decodedClaims.uid);
await saveUserLogin(userRecord)
res.cookie('session', sessionCookie, options)
res.status(200).send({status: "success"})
} catch (err) {
console.error(err)
res.status(401).send('Unauthorized request!');
}
})
I also created the redirect from login page if login successful / refresh in login fail (these things where previously missing)
Also there are many UI improvements made, that’s not written in this Devlog
(●’◡’●)
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.