2023-08-26 00:38:26 +01:00
const logs = require ( "../utils/logs.utils" ) ;
const crypt = require ( "crypto" ) ;
const db = require ( "../services/dbAccess.services" ) ;
const fs = require ( "fs/promises" ) ;
2023-08-02 19:55:25 +01:00
import { minifyMail } from "./minify.utils" ;
2023-05-06 21:54:49 +01:00
// Checks users registration key against issued keys
2023-08-30 21:34:06 +01:00
async function isAuthed ( uuid : string ) : Promise < boolean > {
2023-08-26 00:38:26 +01:00
// Needs testing
2023-09-10 20:49:13 +01:00
const q = {
/*uuid: uuid*/
} ;
2023-08-26 00:38:26 +01:00
const res = await db . query ( "users" , q ) ;
logs . out (
"authUtils.checkUser: DB Query answer: " + JSON . stringify ( res [ 0 ] ) ,
"dbug"
) ;
2023-05-06 21:54:49 +01:00
const authorized = res && res [ 0 ] && res [ 0 ] . domain ;
if ( authorized ) db . userAtime ( uuid ) ;
return authorized ;
}
// Checks whether a registration request key is valid
2023-09-10 21:35:18 +01:00
async function checkRequest ( key : string ) { // For some reason db.query seems to return correctly, but the second logs.out statement prints []??? so registration fails!!
2023-08-26 00:38:26 +01:00
const collection = "registrations" ;
const query = { uuid : key } ;
2023-05-06 21:54:49 +01:00
const res = await db . query ( collection , query ) ;
2023-09-10 21:35:18 +01:00
logs . out ( "authUtils.checkRequest: Raw Result: " + res , "dbug" )
2023-08-26 00:38:26 +01:00
logs . out (
"authUtils.checkRequest: DB Query result: " + JSON . stringify ( res ) ,
"dbug"
) ;
const result =
res . length > 0 && res [ 0 ] . time
? { result : true , domain : res [ 0 ] . domain }
: { result : false } ;
2023-05-06 21:54:49 +01:00
return result ;
}
// Creates an API key for a user
2023-08-26 00:38:26 +01:00
async function generateKey() {
2023-09-10 21:35:18 +01:00
// Needs testing & moving to 'register.utils' ??? Why does it need moving?
2023-07-25 01:00:36 +01:00
return crypt . randomUUID ( ) ;
2023-05-06 21:54:49 +01:00
}
2023-07-24 01:17:00 +01:00
async function generateConfirmationEmail ( eml : string , uuid : string ) {
2023-05-06 21:54:49 +01:00
try {
2023-08-26 00:38:26 +01:00
const htmlTpl = await fs . readFile ( "mail-templates/register.html" , "utf-8" ) ;
2023-05-06 21:54:49 +01:00
const htmlStr = htmlTpl . replace ( />>ACCESSCODE<</g , uuid ) ;
2023-08-02 19:55:25 +01:00
const htmlMin = await minifyMail ( htmlStr ) ;
2023-08-26 00:38:26 +01:00
const txtTpl = fs . readFile ( "mail-templates/register.txt" , "utf-8" ) ;
2023-05-06 21:54:49 +01:00
return {
to : eml ,
2023-08-26 00:38:26 +01:00
subject : "OwlBoard Registration" ,
2023-05-06 21:54:49 +01:00
text : ( await txtTpl ) . replace ( />>ACCESSCODE<</g , uuid ) ,
2023-08-26 00:38:26 +01:00
html : htmlMin ,
2023-05-06 21:54:49 +01:00
} ;
2023-08-26 00:38:26 +01:00
} catch ( err ) {
logs . out (
"mailServices.generateConfirmationEmail: Error reading template, " + err ,
"err"
) ;
2023-05-06 21:54:49 +01:00
return false ;
}
}
module .exports = {
isAuthed ,
generateKey ,
generateConfirmationEmail ,
2023-08-26 00:38:26 +01:00
checkRequest ,
2023-07-25 01:00:36 +01:00
} ;
2023-08-26 00:38:26 +01:00
export { isAuthed , generateKey , generateConfirmationEmail , checkRequest } ;