当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript authentication.AuthenticationService类代码示例

本文整理汇总了TypeScript中@feathersjs/authentication.AuthenticationService的典型用法代码示例。如果您正苦于以下问题:TypeScript AuthenticationService类的具体用法?TypeScript AuthenticationService怎么用?TypeScript AuthenticationService使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了AuthenticationService类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: default

export default (app: Application) => {
  const authentication = new AuthenticationService(app);

  app.set('authentication', {
    entity: 'user',
    service: 'users',
    secret: 'supersecret',
    authStrategies: [ 'local', 'jwt' ],
    local: {
      usernameField: 'email',
      passwordField: 'password'
    }
  });

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());

  app.use('/authentication', authentication);
  app.use('/users', memory({
    paginate: {
      default: 10,
      max: 20
    }
  }));

  app.service('users').hooks({
    before: {
      create: hashPassword('password')
    },
    after: protect('password')
  });

  app.use('/dummy', {
    find (params) {
      return Promise.resolve(params);
    }
  });

  app.service('dummy').hooks({
    before: authenticate('jwt')
  });

  app.service('users').hooks({
    before (context: HookContext) {
      if (context.id !== undefined && context.id !== null) {
        context.id = parseInt(context.id as string, 10);
      }

      return context;
    }
  });

  return app;
};
开发者ID:feathersjs,项目名称:feathers,代码行数:54,代码来源:fixture.ts

示例2: async

    authApp.get('/:name/authenticate', async (req, res, next) => {
      const { name } = req.params;
      const { accessToken, grant } = req.session;
      const service: AuthenticationService = app.service(authService);
      const [ strategy ] = service.getStrategies(name) as OAuthStrategy[];
      const sendResponse = async (data: AuthenticationResult|Error) => {
        try {
          const redirect = await strategy.getRedirect(data);

          if (redirect !== null) {
            res.redirect(redirect);
          } else if (data instanceof Error) {
            throw data;
          } else {
            res.json(data);
          }
        } catch (error) {
          debug('oAuth error', error);
          next(error);
        }
      };

      try {
        const payload = config.defaults.transport === 'session' ?
          grant.response : req.query;

        const params = {
          authStrategies: [ name ],
          authentication: accessToken ? {
            strategy: linkStrategy,
            accessToken
          } : null
        };

        const authentication = {
          strategy: name,
          ...payload
        };

        debug(`Calling ${authService}.create authentication with strategy ${name}`);

        const authResult = await service.create(authentication, params);

        debug('Successful oAuth authentication, sending response');

        await sendResponse(authResult);
      } catch (error) {
        debug('Received oAuth authentication error', error);
        await sendResponse(error);
      }
    });
开发者ID:feathersjs,项目名称:feathers,代码行数:51,代码来源:express.ts

示例3: Error

export const setup = (options: OauthSetupSettings) => (app: Application) => {
  const authPath = options.authService;
  const service: AuthenticationService = app.service(authPath);

  if (!service) {
    throw new Error(`'${authPath}' authentication service must exist before registering @feathersjs/authentication-oauth`);
  }

  const { oauth } = service.configuration;

  if (!oauth) {
    debug(`No oauth configuration found at '${authPath}'. Skipping oAuth setup.`);
    return;
  }

  const { strategyNames } = service;
  const { path = '/auth' } = oauth.defaults;
  const grant = merge({
    defaults: {
      path,
      host: `${app.get('host')}:${app.get('port')}`,
      protocol: app.get('env') === 'production' ? 'https' : 'http',
      transport: 'session'
    }
  }, omit(oauth, 'redirect'));

  each(grant, (value, key) => {
    if (key !== 'defaults') {
      value.callback = value.callback || `${path}/${key}/authenticate`;

      if (!strategyNames.includes(key)) {
        debug(`Registering oAuth default strategy for '${key}'`);
        service.register(key, new OAuthStrategy());
      }
    }
  });

  app.set('grant', grant);
};
开发者ID:feathersjs,项目名称:feathers,代码行数:39,代码来源:index.ts

示例4: getProfile

// @ts-ignore
import memory from 'feathers-memory';

export class TestOAuthStrategy extends OAuthStrategy {
  async getProfile (data: AuthenticationRequest, _params: Params) {
    if (!data.id) {
      throw new Error('Data needs an id');
    }

    return data;
  }
}

const port = 3000;
const app = express(feathers());
const auth = new AuthenticationService(app);

auth.register('jwt', new JWTStrategy());
auth.register('test', new TestOAuthStrategy());

app.configure(rest());
app.set('host', '127.0.0.1');
app.set('port', port);
app.set('authentication', {
  secret: 'supersecret',
  entity: 'user',
  service: 'users',
  authStrategies: [ 'jwt' ],
  oauth: {
    defaults: {
      transport: 'query'
开发者ID:feathersjs,项目名称:feathers,代码行数:31,代码来源:fixture.ts

示例5: each

  each(grant, (value, key) => {
    if (key !== 'defaults') {
      value.callback = value.callback || `${path}/${key}/authenticate`;

      if (!strategyNames.includes(key)) {
        debug(`Registering oAuth default strategy for '${key}'`);
        service.register(key, new OAuthStrategy());
      }
    }
  });
开发者ID:feathersjs,项目名称:feathers,代码行数:10,代码来源:index.ts


注:本文中的@feathersjs/authentication.AuthenticationService类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。