āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/clerk/clerk-docs/reference/fastify/get-auth ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
getAuth()'
description: The getAuth() helper retrieves the current user's authentication state from the request object.
sdk: fastifyThe getAuth() helper retrieves the current user's authentication state from the request object. It returns the Auth object{{ target: '_blank' }}. See the Next.js reference documentation{{ target: '_blank' }} for more examples on how to use the returned Auth object.
getAuth() optionsThe request object.
opts?{acceptsToken: TokenType, treatPendingAsSignedOut: boolean }An optional object that can be used to configure the behavior of the getAuth() function. It accepts the following properties:
treatPendingAsSignedOut?: A boolean that indicates whether to treat pending session status as signed out. Defaults to true.
</Properties>
getAuth() to retrieve the userIdFor examples on how to use getAuth() to perform authorization checks, see the dedicated guide.
The following example uses getAuth() to protect the route based on token type:
(acceptsToken: 'any') from the request.session_token, it logs that the request is from a user session.import Fastify from 'fastify'
import { getAuth } from '@clerk/fastify'
const fastify = Fastify()
fastify.get('/path', (request, reply) => {
// Use `getAuth()` to protect a route based on token type
const authObject = getAuth(req, { acceptsToken: 'any' })
if (authObject.tokenType === 'session_token') {
console.log('This is a session token from a user')
} else {
console.log(`This is a ${authObject.tokenType} token`)
}
})
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā