xenial-black
xenial-black9mo ago

Role Based Authentication And Authorization

i have a rest api backend from which i get the api data like for login i get this response https://example/api/login/ https://example/api/logout/ { "id":1, "role":agent, "token": fjdhenngkgirhngh, }
2 Replies
kapa.ai
kapa.ai9mo ago
Hi @hostashish. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Refine, you can handle authentication and authorization using the AuthProvider and authProvider.login method. You can use this method to get the token from the API and then use Axios interceptors to include the token in the headers of all requests. Here is an example of how you can set up your AuthProvider:
import { AuthProvider } from "@refinedev/core";
import axios from "axios";

export const axiosInstance = axios.create();
export const authProvider: AuthProvider = {
login: async ({ username, password }) => {
const response = await axios.post('<https://example/api/login/',> { username, password });
const { data, status } = response;
if (status === 200) {
localStorage.setItem('token', data.token);

// set header axios instance
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${data.token}`;

return {
success: true,
redirectTo: "/",
};
}

return {
success: false,
error: {
message: "Login failed",
},
};
},
logout: async () => {
localStorage.removeItem('token');
return {
success: true,
redirectTo: "/",
};
},
check: async () => {
const token = localStorage.getItem('token');
if (token) {
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${token}`;
return {
authenticated: true,
};
}

return {
authenticated: false,
logout: true,
error: {
message: "Check failed",
},
redirectTo: "/",
};
},
getPermissions: async () => ({}),
getIdentity: async () => {
const token = localStorage.getItem('token');
if (!token) {
return null;
}

const response = await axios.get('<https://example/api/me',> {
headers: {
Authorization: `Bearer ${token}`
}
});
const { data, status } = response;
if (status === 200) {
return {
id: data.id,
role: data.role,
};
}

return null;
},
};
import { AuthProvider } from "@refinedev/core";
import axios from "axios";

export const axiosInstance = axios.create();
export const authProvider: AuthProvider = {
login: async ({ username, password }) => {
const response = await axios.post('<https://example/api/login/',> { username, password });
const { data, status } = response;
if (status === 200) {
localStorage.setItem('token', data.token);

// set header axios instance
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${data.token}`;

return {
success: true,
redirectTo: "/",
};
}

return {
success: false,
error: {
message: "Login failed",
},
};
},
logout: async () => {
localStorage.removeItem('token');
return {
success: true,
redirectTo: "/",
};
},
check: async () => {
const token = localStorage.getItem('token');
if (token) {
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${token}`;
return {
authenticated: true,
};
}

return {
authenticated: false,
logout: true,
error: {
message: "Check failed",
},
redirectTo: "/",
};
},
getPermissions: async () => ({}),
getIdentity: async () => {
const token = localStorage.getItem('token');
if (!token) {
return null;
}

const response = await axios.get('<https://example/api/me',> {
headers: {
Authorization: `Bearer ${token}`
}
});
const { data, status } = response;
if (status === 200) {
return {
id: data.id,
role: data.role,
};
}

return null;
},
};
In this example, the login method sends a POST request to the login API endpoint with the username and password. If the status code of the response is 200, it saves the token in the local storage and sets the Authorization header for the axios instance. The logout method removes the token from the local storage. The check method checks if the token exists in the local storage. If it does, it sets the Authorization header for the axios instance and returns that the user is authenticated. The getIdentity method sends a GET request to the 'me' API endpoint to get the user's details. If the status code of the response is 200, it returns the user's id and role. You can then use the Authenticated component to render the appropriate components based on the user's authentication status. For more information, you can refer to the Refine documentation on Authentication (https://refine.dev/docs/guides-concepts/authentication/). For role-based authorization, you can define the can method of the accessControlProvider provider. You can use this method to check the user's role and determine what actions they are allowed to perform. For more information, you can refer to the Refine documentation on Authorization (https://refine.dev/docs/guides-concepts/authorization/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@859610618628931614> kapa.ai is still learning and improving, please let us know how it did by reacting below