ratty-blush
ratty-blush5mo ago

How do I send with admin ID?

import { AuthBindings } from '@refinedev/core'
import nookies from 'nookies'

const mockUsers = [
{
id: 1,
email: 'admin@test.com',
password: 'admin',
roles: ['admin'],
},
{
id: 2,
email: 'editor@test.com',
password: 'admin',
roles: ['editor'],
},
{
id: 3,
email: 'demo@test.com',
password: 'admin',
roles: ['demo'],
},
]

export const authProvider: AuthBindings = {
login: async ({ email, password }) => {
// Suppose we actually send a request to the back end here.
const user = mockUsers.find((item) => item.email === email && item.password === password)

if (user) {
nookies.set(null, 'auth', JSON.stringify(user), {
maxAge: 30 * 24 * 60 * 60,
path: '/',
})
return {
success: true,
redirectTo: '/',
}
}

return {
success: false,
error: {
message: 'Login failed',
name: 'Invalid email or password',
},
}
},
import { AuthBindings } from '@refinedev/core'
import nookies from 'nookies'

const mockUsers = [
{
id: 1,
email: 'admin@test.com',
password: 'admin',
roles: ['admin'],
},
{
id: 2,
email: 'editor@test.com',
password: 'admin',
roles: ['editor'],
},
{
id: 3,
email: 'demo@test.com',
password: 'admin',
roles: ['demo'],
},
]

export const authProvider: AuthBindings = {
login: async ({ email, password }) => {
// Suppose we actually send a request to the back end here.
const user = mockUsers.find((item) => item.email === email && item.password === password)

if (user) {
nookies.set(null, 'auth', JSON.stringify(user), {
maxAge: 30 * 24 * 60 * 60,
path: '/',
})
return {
success: true,
redirectTo: '/',
}
}

return {
success: false,
error: {
message: 'Login failed',
name: 'Invalid email or password',
},
}
},
provider this code. I want when API call send with admi Id
8 Replies
kapa.ai
kapa.ai5mo ago
Hi @florent. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To send the admin ID with an API call, you would typically include it in the headers or the body of the request. However, the provided code does not show any API calls being made. It's a mock login function that checks the provided email and password against a local array of users. If you want to include the admin ID in an API call, you would need to modify your API call function. Here's a general example of how you might do it:
axios.post('/api/endpoint', {
id: user.id,
// other data...
})
axios.post('/api/endpoint', {
id: user.id,
// other data...
})
In this example, user.id would be the admin ID you want to send. Please replace '/api/endpoint' with your actual API endpoint. Please note that this is a general example and might need to be adjusted based on your actual use case and API requirements. If you're using a different library for making HTTP requests (like fetch), the syntax would be different. For more specific guidance, you would need to refer to the documentation or examples for the library you're using to make HTTP requests, and the API you're interacting with. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1184365192482652173> kapa.ai is still learning and improving, please let us know how it did by reacting below
ratty-blush
ratty-blush5mo ago
@kapa.ai Is it possible to get the ID value of the logged in administrator from the general page?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, it is possible to get the ID value of the logged in administrator from the general page. You can use the useGetIdentity hook provided by Refine to get the identity of the logged in user. In your case, you have stored the user information in cookies. You can parse this information and use it as needed. Here's an example of how you might do it:
import { useGetIdentity } from "@pankod/refine";
import nookies from 'nookies';

const MyComponent = () => {
const { data } = useGetIdentity();

React.useEffect(() => {
const cookies = nookies.get();
const user = JSON.parse(cookies.auth);
console.log(user.id); // logs the id of the logged in user
}, [data]);

// ...
}
import { useGetIdentity } from "@pankod/refine";
import nookies from 'nookies';

const MyComponent = () => {
const { data } = useGetIdentity();

React.useEffect(() => {
const cookies = nookies.get();
const user = JSON.parse(cookies.auth);
console.log(user.id); // logs the id of the logged in user
}, [data]);

// ...
}
In this example, useGetIdentity is used to get the identity of the logged in user. When the identity data changes (i.e., when a user logs in or out), the useEffect hook runs, getting the user data from the cookies and logging the user's ID. Please note that this is a general example and might need to be adjusted based on your actual use case. For more specific guidance, you would need to refer to the Refine documentation (https://refine.dev/docs/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1184365192482652173> kapa.ai is still learning and improving, please let us know how it did by reacting below
ratty-blush
ratty-blush5mo ago
@kapa.ai Can I use nookies even if I log in as an admin through an external API?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you can use nookies even if you log in as an admin through an external API. The nookies library is used for handling cookies in Next.js applications, and it works independently of how you handle authentication. When you log in through an external API, you would typically receive some sort of authentication token or user information in the response. You can store this information in cookies using nookies for later use. Here's a general example of how you might do it:
import nookies from 'nookies';

// ...

login: async ({ username, password }) => {
// Send a request to the external API
const response = await fetch('<https://my-api.com/login',> {
method: 'POST',
body: JSON.stringify({ username, password }),
});

const data = await response.json();

if (data.success) {
// Store the user information in cookies
nookies.set(null, 'user', JSON.stringify(data.user), {
maxAge: 30 * 24 * 60 * 60,
});

return {
success: true,
};
}

return {
success: false,
};
},
import nookies from 'nookies';

// ...

login: async ({ username, password }) => {
// Send a request to the external API
const response = await fetch('<https://my-api.com/login',> {
method: 'POST',
body: JSON.stringify({ username, password }),
});

const data = await response.json();

if (data.success) {
// Store the user information in cookies
nookies.set(null, 'user', JSON.stringify(data.user), {
maxAge: 30 * 24 * 60 * 60,
});

return {
success: true,
};
}

return {
success: false,
};
},
In this example, a request is sent to an external API to log in. If the login is successful, the user information received in the response is stored in cookies using nookies. Please note that this is a general example and might need to be adjusted based on your actual use case and API requirements. For more specific guidance, you would need to refer to the nookies documentation (https://www.npmjs.com/package/nookies) and the documentation for your external API. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1184365192482652173> kapa.ai is still learning and improving, please let us know how it did by reacting below