msal-with-nuxt3
msal-with-nuxt3 copied to clipboard
Microsoft Authentication Library (MSAL) with Nuxt 3
trafficstars
Microsoft Authentication Library (MSAL) with Nuxt 3 🚀
Demo 🔍
https://github.com/Akash52/msal-with-nuxt3/assets/31063892/77e1351c-df21-4a91-bdd1-7cc53c387d81
Features ✨
- 🔁 Handle Redirect Promise: The plugin handles the redirect promise after login or redirect and obtains the response.
- 🎉 Event Callback on Login Success: An event callback is added to detect login success events, triggering the setup of a token expiration timer.
- ⏰ Token Expiration Timer: A timer is set up to automatically refresh the access token upon expiration.
- 🔄 Refresh Access Token: The plugin can refresh the access token silently using the
acquireTokenSilentmethod. - 🙌 Handle Response: The plugin handles the response after login or redirect and sets up the token expiration timer if the response includes an account.
- 🔑 Acquire Token Silently: The plugin provides a method to acquire the access token silently.
- 🚀 Sign In with Redirect: Users can sign in with redirect using the
signInmethod. - 🔍 Get All MSAL Accounts: The plugin provides a method to retrieve all MSAL accounts.
- 🔒 Check Authentication Status: The
isAuthenticatedmethod allows checking if the user is authenticated. - 👋 Sign Out: Users can sign out using the
signOutmethod.
Installation 🛠️
# Using npm
$ npm install --save @azure/msal-browser
# Using yarn
$ yarn add @azure/msal-browser
Usage 🌟
Register the app on the Azure portal
- Go to https://portal.azure.com/
- Search for Azure Active Directory in your organization and select
App Registrationsfrom the left panel and click onNew registration.
- Register the application by giving your name, selecting supported account types (who can use this application) based on requirements, and clicking register.
- Register redirect URL https://yourapplication.com/ or for development http://localhost:3000/
Step 1 :
Step 2 :
Step 3 :
- Copy the app's essential info, create an
.envfile at the root of your project and set the value of the below .env variables.
Refer to the image above to set the values for CLIENT and Tenet ID in the following ENV.
CLIENTID=
AUTHORITY=https://login.microsoftonline.com
REDIRECT_URI=http://localhost:3000
POSTLOGOUT_REDIRECT_URI=http://localhost:3000
After Configure ENV
Our Final step
Run development server
npm run dev
Official documentation guide for Register an application with the Microsoft identity platform
Repo Tree
📦
├─ .eslintrc.cjs
├─ .gitignore
├─ .npmrc
├─ README.md
├─ assets
│ ├─ Microsoft_logo.svg.png
│ └─ css
│ └─ main.css
├─ components
│ ├─ LoginScreen.vue
│ └─ UserProfile.vue
├─ layouts
│ └─ default.vue
├─ middleware
│ └─ auth.global.ts // Authentication middleware
├─ nuxt.config.ts
├─ package-lock.json
├─ package.json
├─ pages
│ ├─ index.vue
│ └─ login.vue
├─ plugins
│ └─ msal.ts // MSAL plugin file
├─ public
│ └─ favicon.ico
├─ stores
│ └─ auth.global.ts
├─ tailwind.config.js
└─ tsconfig.json
Follow the steps below to make your own MSAL plugin. Alternatively, you can use a boilerplate that covers all necessary features in the msal.ts file.
-
Import the
PublicClientApplicationand other necessary modules from@azure/msal-browser. -
Configure the
msalConfigobject with your authentication settings. -
Create an instance of
PublicClientApplicationusing themsalConfig. -
Implement the various methods and functionalities described in the code snippet.
-
Optionally, provide the
msalobject to the Nuxt.js application using theprovideoption. -
Use the provided functionality methods for authentication and token management in your Nuxt.js application.
License
This project is licensed under the MIT License.