cuHacking logocuHacking DevDocs

auth

Authentication functionality provider.

📦 Overview

What it does

This library is the authentication layer using Lucia Auth. It is directly used by the api library, to perform checks on users on certain routes, acting as middleware.

Key Features

  • Unified authentication approach
  • Easy implementation of additional features
  • Can be used in any application or library
  • Type-safe 😁

How to use

Basic Usage Example

This example creates the TRPC context (object sent from the user to the server with every request). By doing this, the routes can identify if the user is authenticated and can access protected routes.

export async function createTRPCContext(opts: { headers: Headers }) {
  const { session, user } = await auth()
 
  return {
    db,
    session,
    user,
    ...opts,
  }
}

📖 API Reference

auth (Promise)

  • Description: Object used for fetching a user and its session.
  • Return Type: Promise<{user: User, session: Session }>

To use the return types, the function should be awaited like the example above.

Error Handling

Failed authentication will return null values for user and session.


🛠️ Commands

View all commands

To view all commands from the library's project.json, package.json and inheritted:

Terminal
pnpm nx show project auth

Focus library usage

To view/focus on all the apps & libraries which use this library:

Terminal
pnpm nx graph --focus auth

📝 Contributing

Best Practices

Code Formatting

The library should be linted without any errors:

Terminal
pnpm nx run auth:lint

Testing

To run tests for this library:

Terminal
pnpm nx run auth:test

❓ FAQs

What happens if a user is not authenticated?

The client will be redirected to the login page.

Can I add a provider?

This will need to be discussed with the team as it requires every team member to make an auth secret on their development servers.


🛑 Troubleshooting

Common Errors

Authentication does not work!

Make sure to have followed the installation steps properly and set all the callback URLs. Then, see if you placed the correct environment variables in the .env file.

Last updated on

On this page

Edit on GitHubMade with 🩶 for Hackers by Hackers