Skip to main content
Skip to main content

Auth

Queries and mutations listed here are used to send requests to the Admin Auth API Routes.

They allow admin users to manage their session, such as login or log out. You can send authenticated requests for an admin user either using the Cookie header, their API token, or the JWT Token. When you log the admin user in using the authentication hook, Medusa React will automatically attach the cookie header in all subsequent requests.

Related Guide: How to implement user profiles.

Mutations

useAdminLogin

This hook is used to log a User in using their credentials. If the user is authenticated successfully, the cookie is automatically attached to subsequent requests sent with other hooks.

Example

import React from "react"
import { useAdminLogin } from "medusa-react"

const Login = () => {
const adminLogin = useAdminLogin()
// ...

const handleLogin = () => {
adminLogin.mutate({
email: "user@example.com",
password: "supersecret",
}, {
onSuccess: ({ user }) => {
console.log(user)
}
})
}

// ...
}

export default Login

Mutation Function Parameters

AdminPostAuthReqAdminPostAuthReqRequired
The admin's credentials used to log in.

Mutation Function Returned Data

AdminAuthResAdminAuthResRequired
The user's details.

useAdminDeleteSession

This hook is used to Log out the user and remove their authentication session. This will only work if you're using Cookie session for authentication. If the API token is still passed in the header, the user is still authorized to perform admin functionalities in other API Routes.

This hook requires authentication.

Example

import React from "react"
import { useAdminDeleteSession } from "medusa-react"

const Logout = () => {
const adminLogout = useAdminDeleteSession()
// ...

const handleLogout = () => {
adminLogout.mutate(undefined, {
onSuccess: () => {
// user logged out.
}
})
}

// ...
}

export default Logout

Queries

useAdminGetSession

This hook is used to get the currently logged in user's details. Can also be used to check if there is an authenticated user.

This hook requires authentication.

Example

import React from "react"
import { useAdminGetSession } from "medusa-react"

const Profile = () => {
const { user, isLoading } = useAdminGetSession()

return (
<div>
{isLoading && <span>Loading...</span>}
{user && <span>{user.email}</span>}
</div>
)
}

export default Profile

Query Returned Data

userOmit<User, "password_hash">Required
User details.
Was this section helpful?