-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
113a5f9
commit a7385e3
Showing
15 changed files
with
185 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
import { UserEntity } from '../../db/entities'; | ||
import { v4 } from 'uuid'; | ||
import { getUserByUsername, getOrSaveUser } from '../../db/actions/users'; | ||
import { | ||
getUserByUsername, | ||
getOrSaveUser, | ||
updateUserPassword, | ||
} from '../../db/actions/users'; | ||
import { hashPassword } from '../../utils'; | ||
import { compare } from 'bcryptjs'; | ||
|
||
export default async function loginAnonymous( | ||
username: string | ||
): Promise<UserEntity> { | ||
username: string, | ||
password: string | ||
): Promise<UserEntity | null> { | ||
const actualUsername = username.split('^')[0]; | ||
const existingUser = await getUserByUsername(username); | ||
if (existingUser) { | ||
return existingUser; | ||
if (!existingUser) { | ||
const hashedPassword = await hashPassword(password); | ||
const user = new UserEntity(v4(), actualUsername, hashedPassword); | ||
user.username = username; | ||
user.language = 'en'; | ||
|
||
const dbUser = await getOrSaveUser(user); | ||
return dbUser; | ||
} | ||
|
||
if (!existingUser.password) { | ||
const hashedPassword = await hashPassword(password); | ||
const dbUser = await updateUserPassword(existingUser.id, hashedPassword); | ||
return dbUser; | ||
} | ||
const user = new UserEntity(v4(), actualUsername); | ||
user.username = username; | ||
user.language = 'en'; | ||
|
||
const dbUser = await getOrSaveUser(user); | ||
return dbUser; | ||
const isPasswordCorrect = await compare(password, existingUser.password); | ||
|
||
return isPasswordCorrect ? existingUser : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
backend/src/db/migrations/1628773645790-CanDeleteSessions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class CanDeleteSessions1628773645790 implements MigrationInterface { | ||
name = 'CanDeleteSessions1628773645790' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "schema" = $2 AND "name" = $3`, ["VIEW","public","user_view"]); | ||
await queryRunner.query(`DROP VIEW "public"."user_view"`); | ||
await queryRunner.query(`CREATE VIEW "user_view" AS | ||
select | ||
u.id, | ||
u.name, | ||
u."accountType", | ||
u.username, | ||
u.currency, | ||
u."stripeId", | ||
u.photo, | ||
u.language, | ||
u.email, | ||
case when u."accountType" = 'anonymous' and u.password is null then false else true end as "canDeleteSession", | ||
u.trial, | ||
s.id as "ownSubscriptionsId", | ||
s.plan as "ownPlan", | ||
coalesce(s.id, s2.id, s3.id) as "subscriptionsId", | ||
coalesce(s.active, s2.active, s3.active, false) as "pro", | ||
coalesce(s.plan, s2.plan, s3.plan) as "plan", | ||
coalesce(s.domain, s2.domain, s3.domain) as "domain" | ||
from users u | ||
left join subscriptions s on s."ownerId" = u.id and s.active is true | ||
left join subscriptions s2 on lower(u.email) = any(lower(s2.members::text)::text[]) and s2.active is true | ||
left join subscriptions s3 on s3.domain = split_part(u.email, '@', 2) and s3.active is true | ||
`); | ||
await queryRunner.query(`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, ["VIEW","public","user_view","select \n u.id,\n u.name,\n u.\"accountType\",\n u.username,\n u.currency,\n u.\"stripeId\",\n u.photo,\n u.language,\n u.email,\n case when u.\"accountType\" = 'anonymous' and u.password is null then false else true end as \"canDeleteSession\",\n u.trial,\n s.id as \"ownSubscriptionsId\",\n s.plan as \"ownPlan\",\n coalesce(s.id, s2.id, s3.id) as \"subscriptionsId\",\n coalesce(s.active, s2.active, s3.active, false) as \"pro\",\n coalesce(s.plan, s2.plan, s3.plan) as \"plan\",\n coalesce(s.domain, s2.domain, s3.domain) as \"domain\"\nfrom users u \n\nleft join subscriptions s on s.\"ownerId\" = u.id and s.active is true\nleft join subscriptions s2 on lower(u.email) = any(lower(s2.members::text)::text[]) and s2.active is true\nleft join subscriptions s3 on s3.domain = split_part(u.email, '@', 2) and s3.active is true"]); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "schema" = $2 AND "name" = $3`, ["VIEW","public","user_view"]); | ||
await queryRunner.query(`DROP VIEW "user_view"`); | ||
await queryRunner.query(`CREATE VIEW "public"."user_view" AS select | ||
u.id, | ||
u.name, | ||
u."accountType", | ||
u.username, | ||
u.currency, | ||
u."stripeId", | ||
u.photo, | ||
u.language, | ||
u.email, | ||
u.trial, | ||
s.id as "ownSubscriptionsId", | ||
s.plan as "ownPlan", | ||
coalesce(s.id, s2.id, s3.id) as "subscriptionsId", | ||
coalesce(s.active, s2.active, s3.active, false) as "pro", | ||
coalesce(s.plan, s2.plan, s3.plan) as "plan", | ||
coalesce(s.domain, s2.domain, s3.domain) as "domain" | ||
from users u | ||
left join subscriptions s on s."ownerId" = u.id and s.active is true | ||
left join subscriptions s2 on lower(u.email) = any(lower(s2.members::text)::text[]) and s2.active is true | ||
left join subscriptions s3 on s3.domain = split_part(u.email, '@', 2) and s3.active is true`); | ||
await queryRunner.query(`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, ["VIEW","public","user_view","select \n u.id,\n u.name,\n u.\"accountType\",\n u.username,\n u.currency,\n u.\"stripeId\",\n u.photo,\n u.language,\n u.email,\n u.trial,\n s.id as \"ownSubscriptionsId\",\n s.plan as \"ownPlan\",\n coalesce(s.id, s2.id, s3.id) as \"subscriptionsId\",\n coalesce(s.active, s2.active, s3.active, false) as \"pro\",\n coalesce(s.plan, s2.plan, s3.plan) as \"plan\",\n coalesce(s.domain, s2.domain, s3.domain) as \"domain\"\nfrom users u \n\nleft join subscriptions s on s.\"ownerId\" = u.id and s.active is true\nleft join subscriptions s2 on lower(u.email) = any(lower(s2.members::text)::text[]) and s2.active is true\nleft join subscriptions s3 on s3.domain = split_part(u.email, '@', 2) and s3.active is true"]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.