import * as express from 'express' ; import { AuthRepo } from './auth.repository' ; import fetch from 'node-fetch' ; import { CONFIG } from './config' ; import { extractToken } from './utils' ; import { Auth } from './auth.model' ; export class AuthRouter { repo : AuthRepo ; router : express . Router = express . Router ( ) ; serverConfig : CONFIG . IServerConfig ; middlewares : { ( req , res , next ) ; } [ ] = [ ] ; constructor ( datas ) { Object . assign ( this , datas ) ; } async getRoutes ( ) { const MIDDLEWARES = [ ... this . middlewares , ] ; return this . router . get ( '/test' , async ( req , res , next ) => { return res . status ( 200 ) . json ( { code : 200 , message : 'work!' } ) ; } ) . get ( '/isauth' , MIDDLEWARES , async ( req , res , next ) => { let token = extractToken ( req ) ; if ( req . query . loadUserData === undefined ) return res . status ( 200 ) . json ( { token } ) ; if ( ! ( ( < any > req ) . decoded || { } ) . _id ) return next ( { code : 401 , message : 'No user ID in Token' } ) ; const url = `${ this . serverConfig . gateway_host }/users/${ ( ( < any > req ) . decoded || { } ) . _id }` ; const { user , code , message , stack } = await fetch ( url , { method : 'GET' , headers : { 'content-type' : 'application/json' , 'x-access-token' : token , 'x-backend-token' : this . serverConfig . backendToken , } , } ) . then ( async ( response ) => await response . json ( ) . catch ( err => err ) ) . catch ( err => err ) ; if ( ! user ) return next ( { code : code || 400 , message : message || 'User check failed' , stack : user } ) ; token = this . repo . getToken ( { _id : user . uid , email : user . email , password : '' } ) ; return res . status ( 200 ) . json ( { user , token } ) ; } ) . post ( '/' , async ( req , res , next ) => { if ( ! req . body . email ) { return next ( { code : 400 , message : 'Email not provided' } ) ; } if ( ! req . body . password ) { return next ( { code : 400 , message : 'Password not provided' } ) ; } const backendToken = this . serverConfig . backendToken ; const { token = null , auth = null } = await this . repo . auth ( req . body . email , req . body . password ) . catch ( err => err ) ; if ( ! token || ! auth ) return next ( { code : 400 , message : 'Authentication failed no user or token found' , stack : { auth , token } } ) ; const { user , message , code , stack } = await fetch ( `${ this . serverConfig . gateway_host }/users/${ auth . _id }` , { method : 'GET' , headers : { 'content-type' : 'application/json' , 'x-access-token' : token , 'x-backend-token' : backendToken , } , } ) . then ( async ( response ) => await response . json ( ) . catch ( err => err ) ) . catch ( err => err ) ; if ( ! user ) return next ( { code : code || 400 , message : message || 'Authentication failed' , stack } ) ; return res . status ( 200 ) . json ( { user , token } ) ; } ) . post ( '/signin' , async ( req , res , next ) => { const { email , password } = req . body ; if ( ! email || ! password ) return next ( { code : 400 , message : 'Signin failed. Form fiels error' } ) ; const backendToken = this . serverConfig . backendToken ; const response = await this . repo . signin ( { email , password } ) . catch ( err => err ) ; const { auth = null , token = null } = response ; if ( ! auth || ! token ) { return next ( response ) ; } const newUser = new Auth ( req . body ) ; const { user , code , message , stack } = await newUser . createUser ( Object . assign ( { } , { uid : auth . _id } , req . body ) , this . serverConfig . gateway_host , token , backendToken ) . catch ( err => err ) ; if ( code && code !== 200 ) return next ( { code , message , stack } ) ; return res . status ( 200 ) . json ( { user , token } ) ; } ) . put ( '/:aid' , MIDDLEWARES , async ( req , res , next ) => { const result = { } ; const { auth , user } = req . body ; const token = extractToken ( req ) ; const backendToken = this . serverConfig . backendToken ; const requestAuth = await this . repo . update ( auth . _id , auth ) . catch ( err => err ) ; if ( ! requestAuth . auth ) return next ( { code : 400 , message : 'User update failed' } ) ; result . auth = requestAuth . auth ; const requestUser = await fetch ( `${ this . serverConfig . gateway_host }/users/${ user . _id }` , { method : 'PUT' , body : JSON . stringify ( user ) , headers : { 'content-type' : 'application/json' , 'x-access-token' : token || '' , 'x-backend-token' : backendToken || '' , } , } ) . catch ( err => err ) ; if ( requestUser . status === 200 ) return next ( { code : 400 , message : 'User update failed' , stack : requestUser } ) ; result . user = await requestUser . json ( ) . user ; return res . status ( 200 ) . json ( result ) ; } ) . delete ( '/:id' , MIDDLEWARES , async ( req , res , next ) => { await this . repo . delete ( req . params . id ) . then ( auth => res . status ( 200 ) . json ( { auth : auth . auth } ) ) . catch ( ( err ) => next ( { code : 400 , message : err . message , stack : err . errorObj } ) ) ; } ) ; } }