Skip to content

sherlockmac/authentication-strategies

Repository files navigation

Nuxt Authentication Project

A Nuxt 3 project implementing various authentication strategies including JWT, Basic Auth, and OAuth along with SSO explanation. This project is a simple example of how to implement these authentication strategies in a Nuxt 3 application.

Project Structure

The project is structured as follows:

  • app: The main app directory
  • components: The components directory
  • middleware: The middleware directory
  • pages: The pages directory
  • public: The public directory
  • server: The server directory

App

The app is the main entry point for the application. It is located in the app.vue file. The file contains a header with links to the different authentication strategies and a main content area that displays the page content using NuxtPage component which uses the pages directory to display the different pages with the help of the vue-router under the hood.

Components

The project makes use of the following components:

  • JWT Login
  • Session Login
  • OAuth Login

Components follow the Nuxt 3 component auto-import convention you can find them under the components/LoginForm directory. They are used in the pages/index.vue file. When putting the components in a directory the name of the component is the name of the directory plus the name of the file. For example the LoginFormJWT component is located in the components/LoginForm/JWT.vue file.

Client-Side Middleware

Located in middleware/ directory we only have one middleware file auth.ts which is used to protect the routes that are not public.

Client-side middlewares can be used like this:

definePageMeta({
  middleware: ['auth']
})

or the way it is done in the project which is to handle the logic in the middleware file.

Pages

Pages are the main content of the application. They are located in the pages directory. We have index.vue page which is the main page of the application and the other pages are the different authentication strategies. The basic.vue page is the page for the basic authentication strategy. The jwt.vue page is the page for the JWT authentication strategy. The oauth.vue page is the page for the OAuth authentication strategy. The session.vue page is the page for the session authentication strategy. There is also a sso.vue page which is the page for the SSO authentication strategy. Each of these will correspond to an address. For example the basic.vue page can be accessed at http://localhost:3000/basic. And index.vue page can be accessed at http://localhost:3000/ during development.

Public

The public directory contains the static assets of the application.

Server

The server directory contains the server-side code of the application.

Server Middleware

Located in server/middleware/ directory just like the client-side middleware we have one file auth.ts which is used to implement the basic authentication strategy. It checks if the path is /basic and if so it will apply the basic authentication strategy to the request. It will return a 401 Unauthorized error if the authentication fails. We also store the headers in the event context so we could get them in the client.

API Routes

The project provides several authentication-related API endpoints under server/api/:

Session Authentication Endpoints

  • /api/auth/login.post.ts: Handle username/password login
  • /api/auth/logout.post.ts: Handle user logout
  • /api/auth/session.get.ts: Get current session information

JWT Authentication Endpoints

  • /api/auth/jwt/login.post.ts: Handle JWT-based authentication
  • /api/auth/jwt/logout.post.ts: Handle JWT-based logout

GitHub OAuth

  • /api/auth/github/login.get.ts: Initiate GitHub OAuth flow
  • /api/auth/github/callback.get.ts: Handle GitHub OAuth callback

Headers

  • /api/auth/headers.get.ts: Get headers from the request which we store in the event context in the basic authentication middleware.

Setup

Make sure to install dependencies using pnpm:

pnpm install

Development Server

Start the development server on http://localhost:3000:

pnpm dev

Production

Build the application for production:

pnpm build

Locally preview production build:

pnpm preview

Check out the Nuxt documentation for deployment.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published