Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
1
Indexable
Never
import { Request, Response } from 'express';
const jwt = require('jsonwebtoken');
import knex from '@/database';

export default new class Login {
  public async post(request: Request, response: Response) {
    let user;
    let _user_token = false;
    try {
      user = await knex('users')
      .where('email', request.params.email)
      .andWhere('password', request.params.password)
      .first()
      .select('*');
      if (user) {
        const token = jwt.sign({ user_id: user.user_id }, process.env.SECRET, {
          expiresIn: 86400
        });
        const _user_token = {
          token,
          user: {
            userid: user.user_id,
            name: user.name,
            email: user.email,
            usertype: user.usertype,
            idusersabium: user.idusersabium,
          },
        };
      }
      response.status(201).json({ token: _user_token });
    } catch (error) {
      console.error('Error creating user:', error);
      response.status(500).json({ message: 'Internal server error' });
    }
    return _user_token;
  }
}
Leave a Comment