Skip to content

Commit

Permalink
## Modulo Nuxt-Simple-Auth
Browse files Browse the repository at this point in the history
- Nuxt 3
** version 1.3.0 ***

- typeScript
  • Loading branch information
aslan.gama committed Dec 31, 2023
1 parent 83094ff commit 92342a3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Then, add nuxt-simple-auth to the modules section of nuxt.config.js:
``` js
strategies: {
local: {
redirect: {
logout: "/test/logout"
},
token: {
property: 'access_token',
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-simple-auth",
"version": "1.2.9",
"version": "1.3.0",
"description": "Authentication module simple for Nuxt.js",
"repository": "https://github.com/4sllan/nuxt-simple-auth",
"author": "Aslan Gama <@4slan>",
Expand Down
14 changes: 6 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ import type {
} from './runtime/types'


const PACKAGE_NAME = 'nuxt-simple-auth'
const PACKAGE_NAME:string = 'nuxt-simple-auth'
export default defineNuxtModule<ModuleOptions>({

meta: {
name: PACKAGE_NAME,
configKey: 'auth'
},

async setup(options, nuxt: any) {
async setup(options, nuxt) {
const logger = useLogger(PACKAGE_NAME)


const {resolve} = createResolver(import.meta.url)
const auth = nuxt.options?.auth;

nuxt.options.runtimeConfig[PACKAGE_NAME] = auth;
nuxt.options.runtimeConfig[PACKAGE_NAME] = options;

if(auth.cookie && auth.cookie.prefix){
nuxt.options.runtimeConfig.public.prefix = auth.cookie.prefix;
if (options.cookie && options.cookie.prefix) {
nuxt.options.runtimeConfig.public.prefix = options.cookie.prefix;
}

if (auth['2fa']) {
if (options['2fa']) {
//add middleware 2fa
addRouteMiddleware({
name: '_2fa',
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/api/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineEventHandler(async (event) => {

const {cookie, strategies, "2fa": s} = config
const prefix = cookie.prefix && !import.meta.dev ? cookie.prefix : 'auth.'
const {redirect: r, token: t, user: u, endpoints: e} = strategies[strategyName]

if (strategyName) {

Expand All @@ -20,7 +21,7 @@ export default defineEventHandler(async (event) => {
deleteCookie(event, `${prefix}_2fa_expiration.${strategyName}`, cookie.options)
}

return true
return r
}

return false
Expand Down
12 changes: 7 additions & 5 deletions src/runtime/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
async logout(strategyName) {
try {
const {data, pending, error, refresh} = await useFetch('/api/logout', {
baseUrl: siteURL || baseUrl, method: 'POST', body: {strategyName}
baseUrl: siteURL || baseUrl, method: 'POST', body: {strategyName},

onResponse({request, response, options}) {
const {logout} = response._data
return navigateTo(logout ?? '/');
}
});
store.$reset()
sessionStorage.clear()

if (data.value) {
return navigateTo('/');
}
return data.value

} catch (error) {
console.log(error)
Expand Down
16 changes: 6 additions & 10 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export interface ModuleOptions {

}

export interface ModuleHooks {
'my-module:init': any
}
Expand All @@ -17,7 +13,7 @@ export interface ModulePublicRuntimeConfig {
type CookieOption = {
httpOnly?: boolean
secure?: boolean
sameSite?: boolean | string
sameSite?: string
priority?: string
maxAge?: number
domain?: string
Expand All @@ -38,13 +34,13 @@ type EndpointsOptions = {
login: fetchOption
user: fetchOption
"2fa"?: fetchOption
logout?: boolean,
logout?: boolean
}

type redirectOptions = {
login?: string,
logout: string,
callback?: string,
login?: string
logout: string
callback?: string
home?: string
}

Expand All @@ -63,7 +59,7 @@ type AuthOptionsStrategies = {
[key: string]: StrategiesOptions
}

export interface ModuleAuthOptions {
export interface ModuleOptions {
cookie?: AuthOptionsCookie
"2fa"?: boolean
strategies: AuthOptionsStrategies
Expand Down

0 comments on commit 92342a3

Please sign in to comment.