Skip to content

Commit

Permalink
fixup!: more code
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Dec 12, 2018
1 parent fcfdd10 commit 08f6eb5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {RestApplication} from '@loopback/rest';
import {ServiceMixin} from '@loopback/service-proxy';
import {MySequence} from './sequence';
import * as path from 'path';
import {AuthenticationBindings} from '@loopback/authentication';
import {
AuthenticationBindings,
AuthenticationComponent,
} from '@loopback/authentication';
import {JWTProvider} from './providers';

/**
Expand All @@ -36,13 +39,16 @@ export class ShoppingApplication extends BootMixin(
// Bind package.json to the application context
this.bind(PackageKey).to(pkg);

this.component(AuthenticationComponent);

// Set up the custom sequence
this.sequence(MySequence);

// Set up default home page
this.static('/', path.join(__dirname, '../../public'));

this.projectRoot = __dirname;

// Customize @loopback/boot Booter Conventions here
this.bootOptions = {
controllers: {
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {promisify} from 'util';
import * as isemail from 'isemail';
import {RecommenderService} from '../services/recommender.service';
import {inject} from '@loopback/core';
import {authenticate} from '@loopback/authentication';
import {
authenticate,
} from '@loopback/authentication';

const hashAsync = promisify(hash);

Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { ShoppingApplication } from './application';
import { ApplicationConfig } from '@loopback/core';
import { AuthenticationComponent } from '@loopback/authentication';
import {ShoppingApplication} from './application';
import {ApplicationConfig} from '@loopback/core';

export { ShoppingApplication, PackageInfo, PackageKey } from './application';
export {ShoppingApplication, PackageInfo, PackageKey} from './application';

export async function main(options?: ApplicationConfig) {
const app = new ShoppingApplication(options);

app.component(AuthenticationComponent);
// app.component(AuthenticationComponent);

await app.boot();
await app.start();
Expand Down
18 changes: 8 additions & 10 deletions src/providers/JWT.provider.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Provider, ValueOrPromise } from '@loopback/context';
import * as JWT from 'jsonwebtoken';
import {Provider, ValueOrPromise} from '@loopback/context';
const jwt = require('jsonwebtoken');
import { promisify } from 'util';
import { Request } from '@loopback/rest';
import {promisify} from 'util';
import {Request} from '@loopback/rest';
import {
AuthenticateFn,
UserProfile,
AuthenticationMetadata,
AuthenticationBindings,
} from '@loopback/authentication';
import { inject } from '@loopback/context';
import {inject} from '@loopback/context';

const signAsync = promisify(jwt.sign);
const verifyAsync = promisify(jwt.verify);
Expand All @@ -22,7 +21,7 @@ export class JWTProvider implements Provider<AuthenticateFn | undefined> {
constructor(
@inject(AuthenticationBindings.METADATA)
private metadata: AuthenticationMetadata,
) { }
) {}
value(): ValueOrPromise<AuthenticateFn | undefined> {
// NOTE: there should be a function that maps the metadata.strategy to the corresponding provider
// the logic below shouldn't happen in the provider's value()
Expand All @@ -33,8 +32,7 @@ export class JWTProvider implements Provider<AuthenticateFn | undefined> {
const name = this.metadata.strategy;
if (name === 'jwt') {
return req => this.verify(req);
}
else {
} else {
return Promise.reject(`The strategy ${name} is not available.`);
}
}
Expand All @@ -44,8 +42,8 @@ export class JWTProvider implements Provider<AuthenticateFn | undefined> {
// });

// A mock for sign in
const payload = { admin: true }
await signAsync(payload, SECRET, { expiresInMinutes: 5 });
const payload = {admin: true};
await signAsync(payload, SECRET, {expiresInMinutes: 5});
// const token =
// request.body.token ||
// request.query.token ||
Expand Down

0 comments on commit 08f6eb5

Please sign in to comment.