Skip to content

Commit

Permalink
Ssitema LogIn Token fujnciona perfectamente definitivo
Browse files Browse the repository at this point in the history
  • Loading branch information
Danichu15 committed Apr 13, 2020
1 parent 2399b4e commit 877f66b
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AuthInterceptor implements HttpInterceptor{

const cloned = req.clone({
headers: req.headers.set("Authorization",
"Bearer "+ idToken)
"Basic "+ idToken)
});

return next.handle(cloned);
Expand Down
65 changes: 57 additions & 8 deletions Angular/lucia-no-te-enfades-pp/src/app/_servicies/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const BASE_URL = '/api/user/';
export class UsersService {

actualUser: string;
actualUserName: string;
actualPass: string;

redirectToHome: string = "/index";
Expand All @@ -20,31 +21,79 @@ export class UsersService {
logged: boolean = false;

constructor(private http: HttpClient) {
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
this.currentUser = this.currentUserSubject.asObservable();
//this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
//this.currentUser = this.currentUserSubject.asObservable();
}

login(user: string, pass: string, oldUser: boolean) {
/*

var basic = btoa(user + ':' + pass);
localStorage.setItem('id_token' ,basic);

var httpOptions = {
headers: new HttpHeaders({
'X-Requested-With' : 'XMLHttpRequest',
'Authorization': 'Basic ' + btoa(user + ':' + pass)
'Authorization': 'Basic ' + basic
})
};
*/
return this.http.get<any>('/api/logIn').pipe(



return this.http.get<any>('/api/logIn', httpOptions).pipe(
map(user => {
user.authData = window.btoa(user + ':' + pass);
//user.authData = window.btoa(user + ':' + pass);
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
//localStorage.setItem('currentUser', user.authData);
//this.currentUserSubject.next(user);

this.logged = true;
this.actualUser = user;
this.actualUserName = user.username;
this.actualPass = pass;

localStorage.setItem('loggedString' , "true");
localStorage.setItem('actualUserName', this.actualUserName);

})
)
}

logOut():Observable<any>{
return this.http.get<any>('/api/logOut').pipe(
catchError(error => this.handleError(error))
) as Observable<any>
}

logOutProcedure(){
localStorage.setItem('id_token' ,'');

this.logged = false;
this.actualUser = null;
this.actualUserName = null;
this.actualPass = null;

localStorage.setItem('loggedString' , "false");
localStorage.setItem('actualUserName', this.actualUserName);
}

getLogged(){

var loggedS = localStorage.getItem('loggedString');

if(loggedS === "true"){
return true;
}else{
return false;
}
}

getActualUserName(){

var userNameString = localStorage.getItem('actualUserName');

return userNameString;
}

getUserByUserName(userName: String): Observable<User>{
return this.http.get(BASE_URL + "name=" + userName).pipe(
catchError(error => this.handleError(error))
Expand Down
13 changes: 8 additions & 5 deletions Angular/lucia-no-te-enfades-pp/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
<li class="nav-item active">
<a [routerLink]="['/']" class="nav-link">Home</a>
</li>
<li class="nav-item active" [hidden]="logged()">
<a [routerLink]="['/login']" class="nav-link">Sign In</a>
<li class="nav-item active" *ngIf="notLogged">
<a [routerLink]="['/login']" class="nav-link">Sign in</a>
</li>
<div [hidden]="!logged()">
<li class="nav-item active" *ngIf="logged">
<a [routerLink]="['/profile/',userLoggedId]" class="nav-link">{{userLoggedName}}</a>
</div>
</li>
<li class="nav-item active">
<a [routerLink]="['/leaderboard']" class="nav-link">Leaderboard</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<li class="nav-item active" *ngIf="notLogged">
<a [routerLink]="['/']" class="nav-link">Register</a>
</li>
<li class="nav-item active" *ngIf="logged">
<button (click)="logOut()" class="buttonNoBack">Log out</button>
</li>
</ul>
</div>
</nav>
Expand Down
38 changes: 31 additions & 7 deletions Angular/lucia-no-te-enfades-pp/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
styleUrls: ['./home/carousel.component.css']
})
export class AppComponent {
title = 'lucia-no-te-enfades-pp';
Expand All @@ -19,26 +19,50 @@ export class AppComponent {
userLoggedId: number;
imgWebIconUrl = "assets/web-icon-white.png";

constructor(private userService: UsersService){
logged: boolean;
notLogged: boolean;

constructor(private userService: UsersService){

}



logged() { return this.userService.logged; }

ngOnInit(): void {
/*
this.userService.currentUser.subscribe(
response => {
let data: any = response;
this.userLoggedName = data.username;
this.userLoggedId = data.iduser;
}
);
*/

this.userLoggedName = this.userService.getActualUserName();
this.logged = this.userService.getLogged();

if(this.logged){
this.notLogged = false;
}else{
this.notLogged = true;
}

if(this.userLoggedName != "undefined" && this.userLoggedName != "null" && this.userLoggedName != null){
this.setUserId(this.userLoggedName);
}

}

logOut(){
this.userService.logOut().subscribe(
response => {
this.userService.logOutProcedure();
window.location.reload();
},
error => console.error('Error in logOut ' + error)
)
}

getUserId(name: String) {
setUserId(name: String) {
this.userService.getUserByUserName(name).subscribe(
user => {
let data: any = user;
Expand Down
14 changes: 14 additions & 0 deletions Angular/lucia-no-te-enfades-pp/src/app/home/carousel.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,17 @@ body {
margin-top: 30px;
margin-bottom: auto;
}

.buttonNoBack {
color: white;
background-color: rgba(30, 143, 255, 0);
text-align: center;
text-align-last: center;
font-size: 15px;
font-family: "Lucida Sans Unicode";
font-weight: auto;
margin-left: auto;
margin-right: auto;
margin-top: auto;
margin-bottom: auto;
}
17 changes: 2 additions & 15 deletions Angular/lucia-no-te-enfades-pp/src/app/login/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,15 @@

</head>
<body class="text-center">
<form class="form-signin" action="/login" [formGroup]="credentials">
<img class="mb-4" src="/imgs/NavBar/web-icon-black.png" alt=""
width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="inputEmail" class="sr-only">Email address</label>
<input class="form-control" #username formControlName="username" id="username" name="username" placeholder="Email" type="text">
<label for="inputPassword" class="sr-only">Password</label>
<input class="form-control" #password formControlName="password" id="password" name="password" placeholder="Password" type="password" />
<div class="text-center pt-3">
<button class="btn" (click)="login(username.value, password.value, $event)" type="submit">Log in</button>
</div>

</form>

<div class="center">

<form class="form-signin">
<form class="form-signin" action="/login" [formGroup]="credentials">

<img class="mb-4" [src]="imgWebIconUrl" alt="" width="72" height="72">

<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>

<label for="inputEmail" class="sr-only">Email address</label>
<input class="form-control" #username formControlName="username" id="username" name="username" placeholder="Email" type="text">

Expand Down
10 changes: 8 additions & 2 deletions Angular/lucia-no-te-enfades-pp/src/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class LoginComponent implements OnInit {
error: boolean;
returnUrl:string;

loggedIn: boolean;
notLoggedIn: boolean;

imgWebIconUrl ="assets/web-icon-black.png";

constructor(public userService: UsersService, public router: Router, public route:ActivatedRoute) {
Expand All @@ -40,7 +43,8 @@ export class LoginComponent implements OnInit {
}
this.userService.login(username, password,false).subscribe(
res => {
console.log(res);
this.loggedIn = true;
this.notLoggedIn = false;
this.navigate();
},
error => {
Expand All @@ -53,12 +57,14 @@ export class LoginComponent implements OnInit {
}

ngOnInit() {
this.error = false;
this.error = false;
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';//tocar esto

}
navigate() {
this.router.navigate([this.returnUrl]);
//window.history.back();
window.location.reload();
}

// convenience getter for easy access to form fields
Expand Down
2 changes: 0 additions & 2 deletions Angular/lucia-no-te-enfades-pp/src/app/team/team.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { Team } from '../models/team.model';
import { Player } from '../models/player.model';
import { User } from '../models/user.model';

const BASE_URL = 'https://127.0.0.1:8443/api/teams/';

@Component({
selector: 'team',
templateUrl: './team.component.html',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/teamsOnGame/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/teamsOnGame/**").hasRole("ADMIN");

http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/user/**").hasRole("ADMIN");
//http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/user/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/user/**").access("hasRole('USER') or hasRole('ADMIN')");
http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/user/**").hasRole("USER");

http.authorizeRequests().antMatchers(HttpMethod.PUT, "/api/player/**").hasRole("ADMIN");

http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/tournaments/**").hasRole("USER");
//http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/tournaments/**").hasRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/tournaments/**").access("hasRole('USER') or hasRole('ADMIN')");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/tournaments").hasRole("ADMIN");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/tournaments/**").hasRole("USER");

Expand Down

0 comments on commit 877f66b

Please sign in to comment.