Continued work on remember me tokens.

This commit is contained in:
rainbow napkin 2025-10-20 07:49:41 -04:00
parent 95ed2fa403
commit e00e5a608b
11 changed files with 113 additions and 36 deletions

View file

@ -27,7 +27,7 @@ const crypto = require("node:crypto");
const {mongoose} = require('mongoose');
//Local Imports
const userSchema = require('./userSchema');
const {userModel} = require('./userSchema');
const hashUtil = require('../../utils/hashUtils');
const loggerUtils = require('../../utils/loggerUtils');
@ -67,10 +67,14 @@ const rememberMeToken = new mongoose.Schema({
* Pre-Save function for rememberMeSchema
*/
rememberMeToken.pre('save', async function (next){
//Ensure tokens ALWAYS get a new UUID and creation date
this.id = crypto.randomUUID();
this.date = new Date();
//If the token was changed
if(this.isModified("token")){
//Hash that sunnovabitch, no questions asked.
this.token = hashUtil.hashRememberMeToken(this.token);
this.token = await hashUtil.hashRememberMeToken(this.token);
}
//All is good, continue on saving.
@ -79,10 +83,10 @@ rememberMeToken.pre('save', async function (next){
//statics
rememberMeToken.statics.genToken = async function(user, pass){
try{
//Authenticate user and pull document
const userDB = await userSchema.authenticate(user, pass);
//Authenticate user and pull document
const userDB = await userModel.authenticate(user, pass);
try{
//Generate a cryptographically secure string of 32 bytes in hexidecimal
const token = crypto.randomBytes(32).toString('hex');
@ -94,7 +98,7 @@ rememberMeToken.statics.genToken = async function(user, pass){
id: tokenDB.id,
token
};
//If we failed (most likely for bad login)
//If we failed for a non-login reason
}catch(err){
return loggerUtils.localExceptionHandler(err);
}