0
Talk guys, good morning, so I have a problem and I’m not sure how to solve, (maybe it’s in the way I implemented it), well:
i am saving a user and his token every time I log in to my platform, in case someone without token tries to access a route that needs token he cannot and is redirected to the route " / ", however it appears without anything loaded, and I’m not sure how to fix this.
import { Component, OnInit } from "@angular/core";
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
user = JSON.parse(localStorage.getItem('user'));
token = this.user.token;
constructor(private http: HttpClient) { }
ngOnInit() {
}
downloadStudents() {
let url = environment.apiUrl + 'admin/csv';
this.http.get(url, { responseType: "text" })
.subscribe(res => {
let csv = 'data:text/csv;charset=utf-8,' + res;
let data = encodeURI(csv);
let link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', 'alunos.csv');
link.click();
})
}
}
This is my code. And as you can see this is how I get my user and my token,
user = JSON.parse(localStorage.getItem('user'));
token = this.user.token;
Can anyone help me ?
After a search in router Guards.
– Eduardo Vargas
Valeu Eduardo Vargas
– Raphael Melo De Lima