Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UserModule

Hierarchy

Index

Constructors

constructor

Properties

Protected Readonly _app

Application instance

Protected Readonly _id

_id: number

Module ID

Protected Readonly _log

_log: Debugger

Module logger

Protected Readonly _name

_name: string

Module name

Protected _sockets

_sockets: Socket<DefaultEventsMap, DefaultEventsMap>[]

Connected sockets

Accessors

endpoints

initPromise

  • get initPromise(): Promise<any>
  • Returns the init promise to know if the module init is done or not

    Returns Promise<any>

name

  • get name(): string

wait

  • get wait(): boolean

Methods

Protected addEndpoint

Protected addEndpoints

  • addEndpoints(endpoints: Endpoint[]): void

Protected init

  • init(): Promise<any>
  • Init method that is made to be overwritten with any async initialization that the server has to wait to be over before starting

    Returns Promise<any>

    A promise of the async process

    await this.init()
    console.log('Module initialization done.')
    // Do stuff that has to wait the init
    

Protected initialize

  • initialize(autoInit: boolean): void
  • If autoInit is set on true, will run the init async method and put the result of the Promise in a variable that can be get by the application. This will allow the Application to be notified at the end of the init method of the Module If autoInit is set on false, will just put a Promise.resolve() in the init variable

    Parameters

    • autoInit: boolean

      Should the Application run the init method

      initialize(true/false)
      

    Returns void

onSocketJoin

  • onSocketJoin(socket: Socket<DefaultEventsMap, DefaultEventsMap>): any
  • Listener for new client socket connection

    Parameters

    • socket: Socket<DefaultEventsMap, DefaultEventsMap>

      Client socket

      // Application
      onSocketJoin((socket) => {
              this._modules.forEach((module) => {
                  module.onSocketJoin(socket)
              })
      })
      
      // MyModule
      public onSocketJoin(socket) {
              console.log(`Socket ${socket.id} joined`)
      }
      
      // index.ts
      const app = new Application({...})
      ...
      app.registerModule(MyModule)
      ...
      
      // When a socket joins the server
      // console: "Socket xZicnuePG5WehCSKAAAB joined"
      

    Returns any

onSocketLeave

  • onSocketLeave(socket: Socket<DefaultEventsMap, DefaultEventsMap>): any
  • Listener for disconnected client sockets

    Parameters

    • socket: Socket<DefaultEventsMap, DefaultEventsMap>

      Client socket

      // Application
      onSocketJoin((socket) => {
              ...
              socket.on('disconnected', (socket) => {
                  this.modules.forEach((module) => {
                      module.onSocketLeave(socket)
                  })
              })
      })
      
      // MyModule
      public onSocketLeave(socket) {
              console.log(`Socket ${socket.id} leaved`)
      }
      
      // index.ts
      const app = new Application({...})
      ...
      app.registerModule(MyModule)
      ...
      
      // When a socket leaves the server
      // console: "Socket xZicnuePG5WehCSKAAAB leaved"
      

    Returns any

postLoginHandler

  • postLoginHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /login POST route: Attempt to authenticate the user and send its information back

    Query parameters:

    - email <email> (mandatory) - Email of the user
        eg: /login?email=test.test@test.test
    
    - password <password> (mandatory) - Password (clear) of the user
        eg: /login?email=test.test@test.test&password=passw0rd
    

    Response: { message: 'success', user: UserSchema }

    const email = '...'
    const password = '...'
    
    const user = await fetch(`/login?email=${email}&password=${password}`, { method: 'POST' })
            .then((res) => res.json())
    
    if(!user.error) {
            console.log('Login successful !')
    
            localStorage.setItem('user.email', user.email)
            localStorage.setItem('user.token', user.token)
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

postLogoutHandler

  • postLogoutHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /logout POST route: Logout the user if it exists

    Query parameters: - token (mandatory) - Token of the user that is attempting to logout eg: /logout?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIiwiaWF0IjoxNjE4ODIyNDgyfQ.FHDzAl7aWGW9GfUbc2n-B23VPk4aVa6cEdqU3ryvuR4

    Response: { message: 'success', disconnected: true }

    const token = '...'
    
    const res = await fetch(`/logout?token=${token}`, { method: 'POST' })
            .then((res) => res.json())
    
    if(!res.error) {
            console.log('Disconnected successfully !')
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

postRegisterHandler

  • postRegisterHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /register POST route: Attempt to crete a new user of the given type (default: 'USER')

    Query parameters:

    - email <email> (mandatory) - Email of the new user
        eg: /register?email=test.test@test.test
    
    - password <password> (mandatory) - Password (clear) of the new user
        eg: /register?email=test.test@test.test&password=passw0rd
    
    - token <token> (mandatory) - JWT user token of the admin account that is creating the new user
        eg: /register?email=test.test@test.test&password=passw0rd&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIiwiaWF0IjoxNjE4ODIyNDgyfQ.FHDzAl7aWGW9GfUbc2n-B23VPk4aVa6cEdqU3ryvuR4
    
    - type <type> (facultative) - Type of account ('USER' or 'ADMIN')
        eg: /register?email=test.test@test.test&password=passw0rd&type=ADMIN&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIiwiaWF0IjoxNjE4ODIyNDgyfQ.FHDzAl7aWGW9GfUbc2n-B23VPk4aVa6cEdqU3ryvuR4
        eg: /register?email=test.test@test.test&password=passw0rd&type=USER&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIiwiaWF0IjoxNjE4ODIyNDgyfQ.FHDzAl7aWGW9GfUbc2n-B23VPk4aVa6cEdqU3ryvuR4
    

    Response: { message: 'success', user: UserSchema }

    const email = '...'
    const password = '...'
    const token = '...'
    
    const user = await fetch(`/register?email=${email}&password=${password}&token=${token}`, { method: 'POST' })
            .then((res) => res.json())
    
    if(!user.error) {
            console.log('User successfully created !')
    
            console.log(user) // { email: '...', password: '...', type: '...', token: null }
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

Protected registerTask

  • Register a task

    Parameters

    • schedule: TaskSchedule

      Schedule of the task: Date, Moment or cron schedule

    • action: TaskAction

      Action the will be run on schedule

    • start: boolean = true

      Does auto start (default: true)

    Returns Task

    The task

    export class MyModule extends Module {
            constructor(app: Application) {
                ...
    
                // Perform task every day at 10h00
                this.registerTask('* * 10 * * *', this.performMyTask.bind(this), true)
            }
    
            public async performMyTask() {
                ...
            }
    }
    

setWait

  • setWait(wait: boolean): void
  • Set the wait flag to the given value

    Parameters

    • wait: boolean

      New wait flag value

      setWait(true/false)
      

    Returns void

Generated using TypeDoc