Skip to content

Commit

Permalink
Merge branch 'tokenUrlGanta'
Browse files Browse the repository at this point in the history
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
ronaldchavezjortilles committed Jun 19, 2024
2 parents b0267e3 + 8ce5c96 commit d467367
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
9 changes: 9 additions & 0 deletions eda/eda_api/lib/module/thirdParty/thirdParty.router.ts
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;
42 changes: 42 additions & 0 deletions eda/eda_api/lib/module/thirdParty/url/url.controller.ts
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);
}
}
}
10 changes: 10 additions & 0 deletions eda/eda_api/lib/module/thirdParty/url/url.router.ts
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;
3 changes: 3 additions & 0 deletions eda/eda_api/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DataSourceRouter from './module/datasource/datasource.router';
import UploadsRouter from './module/uploads/uploads.router';
import MailRouter from './module/mail/mail.router';
import DocuRouter from './routes/api/api-docs'
import ThirdPartyRouter from './module/thirdParty/thirdParty.router';

const router = express.Router();

Expand All @@ -21,6 +22,8 @@ router.use('/addTable', AddTableRouter );

router.use('/mail', MailRouter);

router.use('/tp', ThirdPartyRouter);

/* ruta per documentació*/
router.use("/api-docs", DocuRouter);

Expand Down
12 changes: 12 additions & 0 deletions eda/eda_app/src/app/services/api/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Swal from 'sweetalert2';
@Injectable()
export class UserService extends ApiService {
private route = '/admin/user';
private routeThirdParty = '/tp/url';

public user: User;
public isAdmin: boolean;
Expand Down Expand Up @@ -116,6 +117,17 @@ export class UserService extends ApiService {
);
}

/** Token sending by the third party through an URL*/
tokenUrl(token: string): Observable<any> {

return this.post(`${this.routeThirdParty}/check`, {token: token}, true)
.pipe(map((res: any) => {
this.savingStorage(res.id, res.token, res.user);
return true;
}, (err) =>this.alertService.addError(err))
);
}

/** Renovar Token */
refreshToken() {
return this.get( `${this.route}/refresh-token` )
Expand Down
34 changes: 18 additions & 16 deletions eda/eda_app/src/app/services/guards/login-guard.guard.ts
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;
}

}

}
}

0 comments on commit d467367

Please sign in to comment.