Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** LOGIN COMPONENT **/
- tryLogin() {
- this.apiService.login(
- this.username,
- this.password
- ).subscribe(
- r => {
- this.apiService.setToken(r);
- this.router.navigate(['members','dashboard']);
- },
- r => {
- this.presentAlert({msg: "Nome utente o password non validi, riprovare.", type: "Login error"});
- });
- }
- /** SERVICE INJECTET **/
- constructor(private http: HttpClient, private storage: Storage, private plt: Platform, private router: Router) {
- this.plt.ready().then(() => {
- this.getToken();
- this.loadLocalSettings();
- this.storage.get(TOKEN).then(token => {
- if (token) {
- this.token = token;
- }
- });
- })
- }
- /** Login function */
- login(username: string, password: string): Observable<Login>{
- this.loadLocalSettings();
- return this.http.post<Login>(this.localSettings.apiUrl + 'login', {
- username: username,
- password: password
- });
- }
- setToken(user: User) {
- this.token = user.token;
- this.storage.set(TOKEN, user.token).then((us) => {
- this.setCurrentUser(user);
- });
- this.loggedIn.next(true);
- }
- intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
- request = request.clone({
- setHeaders: {
- Authorization: `Bearer ${this.token}`,
- },
- responseType: 'json'
- });
- return next.handle(request);
- }
Advertisement
Add Comment
Please, Sign In to add comment