I have a router guard:
import { CanActivateFn } from '@angular/router';
import axios from 'axios';
export const loginAuthGuard: CanActivateFn = (route, state) => {
return true;
};
and service with verifyToken function:
import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import axios from 'axios';
axios.defaults.withCredentials = true;
@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor(private cookieService: CookieService) {}
url = 'http://localhost:8000/api/v1/login';
token: string = '';
async login(formObject: any) {
try {
const res = await axios.post(`${this.url}`, formObject, {
withCredentials: true,
});
console.log('http cookie set!');
} catch (error) {
console.error(error);
}
}
async verifyToken() {
try {
const res = await axios.get(`${this.url}`, {
withCredentials: true,
});
} catch (error) {
console.error(error);
}
}
}
If i want to enter a resource, the request is send to the endpoint using validateCookie. If it is true, i can access it, if not, i cant. How to implement it using CanActivateFn
?