/
auth.component.ts
117 lines (108 loc) · 3.28 KB
/
auth.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Component, OnInit } from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
declare let window: any;
@Component({
selector: 'auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.css'],
providers: [AuthService]
})
export class AuthComponent implements OnInit {
login: string;
pass: string;
ticket: string = null;
url: string = null;
sid: string = null;
constructor(
private serv: AuthService,
private router: Router,
private activateRoute: ActivatedRoute
) {
this.ticket = activateRoute.snapshot.params['t'];
this.url = activateRoute.snapshot.params['u'];
this.sid = activateRoute.snapshot.params['s'];
}
ngOnInit(): void {
this.login = '';
this.pass = '';
if (this.ticket) {
this.serv.ticket(this.ticket).subscribe(
(data: any) => {
console.log("Access Token [" + data.access_token + "]");
localStorage.setItem('myAuthToken', data.access_token);
localStorage.setItem('myRole', data.role);
if (this.url && this.sid) {
window.location = 'dagaz/' + this.url +'?sid=' + this.sid;
} else {
this.router.navigate(['session/2']);
}
},
(error: any) => {
if (this.canRecovery()) {
this.recovery();
} else {
this.router.navigate(['auth']);
}
}
);
}
}
onKeydownEvent(e: KeyboardEvent): void {
if (e.key === "Enter") {
this.submit();
}
}
canRecovery(): boolean {
return localStorage.getItem('myAuthToken') !== null;
}
recovery(): void {
const t = localStorage.getItem('myAuthToken');
this.serv.recovery(t).subscribe(
(data: any) => {
console.log("Access Token [" + data.access_token + "]");
localStorage.setItem('myAuthToken', data.access_token);
localStorage.setItem('myRole', data.role);
if (this.url && this.sid) {
window.location = 'dagaz/' + this.url +'?sid=' + this.sid;
} else {
this.router.navigate(['profile']);
}
},
(error: any) => {
alert("Password recovery failed");
}
);
}
submit(): void {
// localStorage.removeItem('myAuthToken');
this.serv.auth(this.login, this.pass).subscribe(
(data: any) => {
console.log("Access Token [" + data.access_token + "]");
localStorage.setItem('myAuthToken', data.access_token);
localStorage.setItem('myRole', data.role);
localStorage.setItem('myId', data.user_id);
localStorage.setItem('myFlags', data.flags);
if (data.realm == 2) {
this.router.navigate(['bonus']);
} else {
const url = localStorage.getItem('currGame');
if (url) {
localStorage.setItem('currGame', 'launch');
this.router.navigate([url]);
} else {
this.router.navigate(['launch']);
}
}
},
(error: any) => {
let status = error.status;
if (status == 401) {
alert("Login or password not found");
} else {
alert("Error: " + status);
}
});
}
}