Browse Source

feat: Allow email or username in local authentication

Owen Diffey 3 months ago
parent
commit
227e4e6283

+ 1 - 1
frontend/src/components/modals/Login.vue

@@ -30,7 +30,7 @@ const submitModal = () => {
 
 	authStore.authenticate({
 		strategy: "local",
-		email: email.value,
+		identifier: email.value,
 		password: password.value.value
 	})
 		.then(() => {

+ 9 - 1
frontend/src/stores/auth.ts

@@ -1,9 +1,17 @@
 import { acceptHMRUpdate, defineStore } from 'pinia'
 import { api } from '@/feathers'
 import { useAuth } from 'feathers-pinia'
+import { User } from 'musare-server';
+
+export interface AuthenticateData {
+  strategy: 'jwt' | 'local';
+  accessToken?: string;
+  identifier?: string;
+  password?: string;
+}
 
 export const useAuthStore = defineStore('auth', () => {
-  return useAuth({ api, servicePath: 'users' });
+  return useAuth<AuthenticateData, User>({ api, servicePath: 'users' });
 });
 
 if (import.meta.hot)

+ 16 - 1
server/src/services/authentication/authentication.ts

@@ -1,9 +1,10 @@
 // For more information about this file see https://dove.feathersjs.com/guides/cli/authentication.html
 import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication'
-import { LocalStrategy } from '@feathersjs/authentication-local'
+import { LocalStrategy as FeathersLocalStrategy } from '@feathersjs/authentication-local'
 
 import type { Application } from '../../declarations'
 import authenticationHooks from './authentication.hooks'
+import { Params, Query } from '@feathersjs/feathers'
 
 declare module '../../declarations' {
   interface ServiceTypes {
@@ -11,7 +12,21 @@ declare module '../../declarations' {
   }
 }
 
+class LocalStrategy extends FeathersLocalStrategy {
+  async getEntityQuery(query: Query, params: Params) {
+    const key = query.identifier.indexOf('@') !== -1 ? 'email' : 'username';
+    return {
+      [key]: query.identifier,
+      $limit: 1
+    }
+  }
+}
+
 export const authentication = (app: Application) => {
+  const config = app.get('authentication')
+  config!.local!.usernameField = 'identifier';
+  app.set('authentication', config)
+
   const authentication = new AuthenticationService(app)
 
   authentication.register('jwt', new JWTStrategy())