angular-10-role-based-authorization-example
angular-10-role-based-authorization-example copied to clipboard
Error in fake-backend.ts file while using it for Angular 14
Hi Sir , There are 2 errors showing in my Angular 14 project when I am trying to run your project in my pc..
// route function
function authenticate(){
const { username, password} = body;
const user = users.find(x => x.username === username && x.password === password);
if(!user) return error('Username or password is incorrect');
return ok({
id: user.id,
username: user.username,
firstName: user.firstName,
lastName: user.lastName,
role: user.role,
token: `fake-jwt-token.${user.id}`
});
}
Error 1 : Argument of type '{ id: number; username: string; firstName: string; lastName: string; role: Role; token: string; }' is not assignable to parameter of type '{ id: number; username: string; password: string; firstName: string; lastName: string; role: Role; } | { id: number; username: string; password: string; firstName: string; lastName: string; role: Role; }[] | undefined'. Object literal may only specify known properties, and 'token' does not exist in type '{ id: number; username: string; password: string; firstName: string; lastName: string; role: Role; } | { id: number; username: string; password: string; firstName: string; lastName: string; role: Role; }[]'.ts(2345)
I am getting above error for " [token :](token: fake-jwt-token.${user.id}
) " this line...
2nd function under fake-backend.ts
function getUserById(){
if(!isLoggedIn()) return unauthorized();
// only admins can access other user records
if(!isAdmin() && currentUser().id !== idFromUrl()) return unauthorized();
const user = users.find(x => x.id === idFromUrl());
return ok(user);
}
Error 2 :
Object is possibly 'undefined'.ts(2532)