本文整理匯總了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;
};
示例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);
}
});
示例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);
};
示例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'
示例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());
}
}
});