-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
se habilita la posibilidad de pasar el token por la url e ir directamente a un dashboard sin pasar por el login.
- Loading branch information
Showing
6 changed files
with
94 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import express from 'express'; | ||
import UrlRouter from './url/url.router'; | ||
|
||
const router = express.Router(); | ||
|
||
router.use('/url', UrlRouter); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const SEED = require('../../../../config/seed').SEED; | ||
|
||
|
||
export class UrlController { | ||
|
||
static async urlCheck(req: Request, res: Response, next: NextFunction) { | ||
|
||
try { | ||
const token = req.body.token; | ||
let user; | ||
|
||
// verificacion del token y extracción del user | ||
user = await jwt.verify(token, SEED, (err, decoded) => { | ||
|
||
if (err) { | ||
console.log(err); | ||
return null | ||
} | ||
|
||
return decoded.user; | ||
}) | ||
|
||
if(user) { | ||
return res.status(200).json({ user, token: token, id: user._id }); | ||
} | ||
|
||
else { | ||
return res.status(400).json({ | ||
ok: false, | ||
token: 'Token invalido o caducado' | ||
}) | ||
} | ||
|
||
} catch (err) { | ||
console.log(err); | ||
next(err); | ||
} | ||
} | ||
} |
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,10 @@ | ||
|
||
import * as express from 'express'; | ||
import { UrlController } from './url.controller'; | ||
const router = express.Router(); | ||
|
||
// ruta -> /tp/url | ||
router.post('/check', UrlController.urlCheck); | ||
|
||
|
||
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
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 |
---|---|---|
@@ -1,32 +1,34 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute } from '@angular/router'; | ||
import { UserService } from '../api/user.service'; | ||
|
||
@Injectable() | ||
export class LoginGuardGuard implements CanActivate { | ||
|
||
constructor(public userService: UserService, | ||
constructor( | ||
public userService: UserService, | ||
private route: ActivatedRoute, | ||
public router: Router) { } | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
|
||
|
||
console.log( route); | ||
if(route.queryParams.token){ | ||
console.log('aqui tengo el token!!!!!'); | ||
console.log('llamo a la api y recupero los valores del usuario'); | ||
// savingStorage( id: string, token: string, user: User) ; | ||
// savingStorage( id de usuario 135792467811111111111111, token: el token que me llega, user: el objeto user del mongo) ; | ||
} | ||
|
||
|
||
|
||
|
||
const token = route.queryParams.token; | ||
|
||
if (this.userService.isLogged()) { | ||
return true; | ||
} else { | ||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url.split('?')[0], params: state.url.split('?')[1] } }); | ||
return false; | ||
if(token){ | ||
this.userService.tokenUrl(token).subscribe(() => { | ||
const urlInforme = state.url.split('?')[0]; | ||
this.router.navigate([urlInforme], { queryParams: route.queryParams }); | ||
}) | ||
return false; | ||
} else { | ||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url.split('?')[0], params: state.url.split('?')[1] } }); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
} | ||
} |