Browse Source

chore: lint

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5572/head
Pranav C 2 years ago
parent
commit
70fab17f9c
  1. 42
      packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts

42
packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts

@ -1,47 +1,45 @@
import { Injectable } from '@nestjs/common' import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport' import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-custom' import { Strategy } from 'passport-custom';
import { ApiToken, ProjectUser, User } from '../../models' import { ApiToken, ProjectUser, User } from '../../models';
@Injectable() @Injectable()
export class AuthTokenStrategy extends PassportStrategy(Strategy, 'authtoken') { export class AuthTokenStrategy extends PassportStrategy(Strategy, 'authtoken') {
// eslint-disable-next-line @typescript-eslint/ban-types
async validate(req: any, callback: Function) { async validate(req: any, callback: Function) {
try { try {
let user let user;
if (req.headers['xc-token']) { if (req.headers['xc-token']) {
const apiToken = await ApiToken.getByToken(req.headers['xc-token']);
const apiToken = await ApiToken.getByToken(
req.headers['xc-token'],
)
if (!apiToken) { if (!apiToken) {
return callback({ msg: 'Invalid token' }) return callback({ msg: 'Invalid token' });
} }
user = {} user = {};
if (!apiToken.fk_user_id) { if (!apiToken.fk_user_id) {
user.roles = 'editor' user.roles = 'editor';
return callback(null, user) return callback(null, user);
} }
const dbUser: Record<string, any> = await User.get(apiToken.fk_user_id) const dbUser: Record<string, any> = await User.get(apiToken.fk_user_id);
if (!dbUser) { if (!dbUser) {
return callback({ msg: 'User not found' }) return callback({ msg: 'User not found' });
} }
dbUser.is_api_token = true dbUser.is_api_token = true;
if (req['ncProjectId']) { if (req['ncProjectId']) {
const projectUser = await ProjectUser.get( const projectUser = await ProjectUser.get(
req['ncProjectId'], req['ncProjectId'],
dbUser.id, dbUser.id,
) );
user.roles = projectUser?.roles || dbUser.roles user.roles = projectUser?.roles || dbUser.roles;
user.roles = user.roles === 'owner' ? 'owner,creator' : user.roles user.roles = user.roles === 'owner' ? 'owner,creator' : user.roles;
return callback(null, user) return callback(null, user);
} }
} }
return callback(null, user) return callback(null, user);
} catch (error) { } catch (error) {
callback(error) callback(error);
} }
} }
} }

Loading…
Cancel
Save