Skip to content

Commit

Permalink
feat: support loging in with username
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Aug 24, 2024
1 parent 9ac691e commit 536b7ec
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion extensions/authentication-jwt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build": "lb-tsc",
"clean": "lb-clean loopback-authentication-jwt*.tgz dist *.tsbuildinfo package",
"pretest": "npm run build",
"test": "lb-mocha \"dist/__tests__/unit/*.js\" \"dist/__tests__/acceptance/*.js\"",
"test": "lb-mocha \"dist/__tests__/acceptance/jwt.component.test.js\"",
"verify": "npm pack && tar xf loopback-authentication-jwt*.tgz && tree package && npm run clean"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe('jwt authentication', () => {
token = res.body.token;
});

it(`user login successfully with username and password`, async () => {
const credentials = {username: 'Jane', password: 'opensesame'};
await client.post('/users/login').send(credentials).expect(200);
});

it('whoAmI returns the login user id', async () => {
const res = await client
.get('/whoAmI')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ const RefreshGrantRequestBody = {
// Describe the schema of user credentials
const CredentialsSchema: SchemaObject = {
type: 'object',
required: ['email', 'password'],
required: ['password'],
properties: {
email: {
type: 'string',
format: 'email',
},
username: {
type: 'string',
},
password: {
type: 'string',
minLength: 8,
Expand Down
20 changes: 16 additions & 4 deletions extensions/authentication-jwt/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {UserRepository} from '../repositories';
* using the email and password. You can modify it if your app has different credential fields
*/
export type Credentials = {
email: string;
email?: string;
username?: string;
password: string;
};

Expand All @@ -26,10 +27,21 @@ export class MyUserService implements UserService<User, Credentials> {
) {}

async verifyCredentials(credentials: Credentials): Promise<User> {
const invalidCredentialsError = 'Invalid email or password.';

const invalidCredentialsError = 'Invalid username/email or password.';
if (!credentials.email && !credentials.username) {
throw new HttpErrors.Unauthorized(
'please provide either username or email.',
);
}
const whereFilter: {email?: string; username?: string} = {};
if (credentials.email) {
whereFilter.email = credentials.email;
}
if (credentials.username) {
whereFilter.username = credentials.username;
}
const foundUser = await this.userRepository.findOne({
where: {email: credentials.email},
where: whereFilter,
});
if (!foundUser) {
throw new HttpErrors.Unauthorized(invalidCredentialsError);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/fixtures/copyright/single-package/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// Copyright ACME Inc. 2020,2024. All Rights Reserved.
// Node module: myapp
// This file is licensed under the ISC License.
// License text available at https://www.isc.org/licenses/

// Use the same set of files (except index.js) in this folder
const files = require('../index')(__dirname);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// Copyright ACME Inc. 2020,2024. All Rights Reserved.
// Node module: myapp
// This file is licensed under the ISC License.
// License text available at https://www.isc.org/licenses/

// XYZ
exports.xyz = {};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Copyright ACME Inc. 2020,2024. All Rights Reserved.
// Node module: myapp
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// This file is licensed under the ISC License.
// License text available at https://www.isc.org/licenses/

export class MyApplication {}

0 comments on commit 536b7ec

Please sign in to comment.