-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cerradogo#14
- Loading branch information
Showing
7 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Request, Response} from 'express' | ||
import authentication from '../helpers/authentication' | ||
const key = "teste" | ||
const create = async (req: Request, res: Response) => { | ||
console.log('teste') | ||
const {username, password} = req.body | ||
try{ | ||
const token = await authentication.auth({username, password}, key) | ||
res.send(token) | ||
} catch(e){ | ||
console.error(e) | ||
res.status(401).end() | ||
} | ||
} | ||
|
||
export default create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {verify, sign} from 'jsonwebtoken' | ||
import UserModel from '../models/User' | ||
import { CustomError } from 'express-handler-errors' | ||
class Auth{ | ||
async auth(user: {username: string; password: string}, key: string): Promise<{token: string}>{ | ||
const {username, password} = user | ||
try{ | ||
const res = await UserModel.findOne({nickname: username, password}) | ||
if (!res) { | ||
throw new CustomError({ | ||
code: 'USER_NOT_FOUND', | ||
message: 'Usuario não foi encontrado', | ||
status: 404 | ||
}) | ||
} | ||
|
||
const token = await sign({ | ||
_id: res._id, | ||
name: res.name | ||
}, key, { | ||
expiresIn: 3000 | ||
}) | ||
return {token} | ||
|
||
} catch (e){ | ||
console.error(e) | ||
throw new Error() | ||
} | ||
} | ||
|
||
validate(token: string, key: string): boolean{ | ||
const jwt = token.split(' ')[1] | ||
let isValid = false | ||
verify(jwt, key, (err)=>{ | ||
if(err) { | ||
return | ||
} | ||
isValid = true | ||
}) | ||
return isValid | ||
} | ||
} | ||
|
||
export default new Auth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Request, Response, NextFunction} from 'express' | ||
import authentication from '../helpers/authentication' | ||
const key = "teste" | ||
const create = async (req: Request, res: Response, next: NextFunction) => { | ||
const token: string | undefined = req.headers.authorization | ||
if(!token){ | ||
res.status(401).send('Não autorizado') | ||
return | ||
} | ||
const isValid = authentication.validate(token, key) | ||
if(isValid){ | ||
next() | ||
} else { | ||
res.status(401).send('Token Invalido') | ||
return | ||
} | ||
|
||
} | ||
|
||
export default create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import express, {Router} from 'express' | ||
import Login from '../controllers/Login' | ||
const router: Router = express.Router() | ||
router.post('/', Login) | ||
//router.post('/validate', validate) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters