Skip to content

Commit

Permalink
Merge pull request #7826 from ever-co/revert-7823-feat/ever-teams-soc…
Browse files Browse the repository at this point in the history
…ial-logins

Revert "Add a service for social logins on Ever Teams"
  • Loading branch information
rahul-rocket committed May 24, 2024
2 parents 82744e5 + 133bd9d commit 32d1025
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 128 deletions.
41 changes: 18 additions & 23 deletions packages/core/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class AuthController {
private readonly authService: AuthService,
private readonly userService: UserService,
private readonly commandBus: CommandBus
) {}
) { }

/**
* Check if the user is authenticated.
Expand Down Expand Up @@ -162,23 +162,6 @@ export class AuthController {
);
}

/**
* Sign in workspaces by email social media.
*
* @param input - User sign-in data.
* @returns
*/
@HttpCode(HttpStatus.OK)
@Post('/signin.email.social')
@Public()
@UseValidationPipe()
async signinWorkspacesBySocial(
@Query() query: Record<string, boolean>,
@Body() input: { email: string }
): Promise<IUserSigninWorkspaceResponse> {
return await this.authService.signinWorkspacesByEmailSocial(input, convertNativeParameters(query.includeTeams));
}

/**
* Send a workspace sign-in code by email.
*
Expand All @@ -190,8 +173,13 @@ export class AuthController {
@Post('/signin.email')
@Public()
@UseValidationPipe({ transform: true })
async sendWorkspaceSigninCode(@Body() entity: UserEmailDTO, @I18nLang() locale: LanguagesEnum): Promise<any> {
return await this.commandBus.execute(new WorkspaceSigninSendCodeCommand(entity, locale));
async sendWorkspaceSigninCode(
@Body() entity: UserEmailDTO,
@I18nLang() locale: LanguagesEnum
): Promise<any> {
return await this.commandBus.execute(
new WorkspaceSigninSendCodeCommand(entity, locale)
);
}

/**
Expand All @@ -207,7 +195,10 @@ export class AuthController {
@Query() query: Record<string, boolean>,
@Body() input: WorkspaceSigninEmailVerifyDTO
): Promise<IUserSigninWorkspaceResponse> {
return await this.authService.confirmWorkspaceSigninByCode(input, convertNativeParameters(query.includeTeams));
return await this.authService.confirmWorkspaceSigninByCode(
input,
convertNativeParameters(query.includeTeams)
);
}

/**
Expand All @@ -219,8 +210,12 @@ export class AuthController {
@Post('/signin.workspace')
@Public()
@UseValidationPipe({ whitelist: true })
async signinWorkspaceByToken(@Body() input: WorkspaceSigninDTO): Promise<IAuthResponse | null> {
return await this.commandBus.execute(new WorkspaceSigninVerifyTokenCommand(input));
async signinWorkspaceByToken(
@Body() input: WorkspaceSigninDTO
): Promise<IAuthResponse | null> {
return await this.commandBus.execute(
new WorkspaceSigninVerifyTokenCommand(input)
);
}

/**
Expand Down
134 changes: 29 additions & 105 deletions packages/core/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ export class AuthService extends SocialAuthService {

/** Fetching users matching the query */
let users = await this.userService.find({
where: [
{
email,
isActive: true,
isArchived: false,
hash: Not(IsNull())
}
],
where: [{
email,
isActive: true,
isArchived: false,
hash: Not(IsNull())
}],
relations: { tenant: true },
order: { createdAt: 'DESC' }
});
Expand All @@ -160,85 +158,16 @@ export class AuthService extends SocialAuthService {
// Update all users with a single query
const ids = users.map((user: IUser) => user.id);

await this.typeOrmUserRepository.update(
{
id: In(ids),
email,
isActive: true,
isArchived: false
},
{
code,
codeExpireAt
}
);

// Determining the response based on the number of matching users
const response: IUserSigninWorkspaceResponse = await this.createUserSigninWorkspaceResponse({
users,
code,
await this.typeOrmUserRepository.update({
id: In(ids),
email,
includeTeams
});

if (response.total_workspaces > 0) {
return response;
} else {
console.log('Error while signin workspace: %s');
throw new UnauthorizedException();
}
}

/**
* Authenticate a user by email from social media and return user workspaces.
*
* @param email - The user's email.
* @param password - The user's password.
* @returns A promise that resolves to a response with user workspaces.
* @throws UnauthorizedException if authentication fails.
*/
async signinWorkspacesByEmailSocial(
input: { email: string },
includeTeams: boolean
): Promise<IUserSigninWorkspaceResponse> {
const { email } = input;

/** Fetching users matching the query */
let users = await this.userService.find({
where: [
{
email,
isActive: true,
isArchived: false
}
],
relations: { tenant: true },
order: { createdAt: 'DESC' }
isActive: true,
isArchived: false
}, {
code,
codeExpireAt
});

if (users.length === 0) {
throw new UnauthorizedException();
}

const code = generateRandomAlphaNumericCode(ALPHA_NUMERIC_CODE_LENGTH);
const codeExpireAt = moment().add(environment.MAGIC_CODE_EXPIRATION_TIME, 'seconds').toDate();

// Update all users with a single query
const ids = users.map((user: IUser) => user.id);

await this.typeOrmUserRepository.update(
{
id: In(ids),
email,
isActive: true,
isArchived: false
},
{
code,
codeExpireAt
}
);

// Determining the response based on the number of matching users
const response: IUserSigninWorkspaceResponse = await this.createUserSigninWorkspaceResponse({
users,
Expand Down Expand Up @@ -912,20 +841,17 @@ export class AuthService extends SocialAuthService {
relations: { role: true }
});

await this.typeOrmUserRepository.update(
{
email,
id: userId,
tenantId,
code,
isActive: true,
isArchived: false
},
{
code: null,
codeExpireAt: null
}
);
await this.typeOrmUserRepository.update({
email,
id: userId,
tenantId,
code,
isActive: true,
isArchived: false
}, {
code: null,
codeExpireAt: null
});

// Retrieve the employee details associated with the user.
const employee = await this.employeeService.findOneByUserId(user.id);
Expand Down Expand Up @@ -1143,13 +1069,11 @@ export class AuthService extends SocialAuthService {
email: user.email || null, // Sets email to null if it's undefined
name: user.name || null, // Sets name to null if it's undefined
imageUrl: user.imageUrl || null, // Sets imageUrl to null if it's undefined
tenant: user.tenant
? new Tenant({
id: user.tenant.id, // Assuming tenantId is a direct property of tenant
name: user.tenant.name || '', // Defaulting to an empty string if name is undefined
logo: user.tenant.logo || '' // Defaulting to an empty string if logo is undefined
})
: null // Sets tenant to null if user.tenant is undefined
tenant: user.tenant ? new Tenant({
id: user.tenant.id, // Assuming tenantId is a direct property of tenant
name: user.tenant.name || '', // Defaulting to an empty string if name is undefined
logo: user.tenant.logo || '' // Defaulting to an empty string if logo is undefined
}) : null // Sets tenant to null if user.tenant is undefined
});
}
}

0 comments on commit 32d1025

Please sign in to comment.