forked from cerc-io/snowballtools-base
Save gitHub token in DB and unauthenticate on expiry (#58)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { DataSource, DeepPartial } from 'typeorm';
|
||||
import path from 'path';
|
||||
import debug from 'debug';
|
||||
import assert from 'assert';
|
||||
|
||||
import { DatabaseConfig } from './config';
|
||||
import { User } from './entity/User';
|
||||
@@ -14,6 +15,7 @@ import { Domain } from './entity/Domain';
|
||||
|
||||
const log = debug('snowball:database');
|
||||
|
||||
// TODO: Fix order of methods
|
||||
export class Database {
|
||||
private dataSource: DataSource;
|
||||
|
||||
@@ -41,6 +43,14 @@ export class Database {
|
||||
return user;
|
||||
}
|
||||
|
||||
async updateUser (userId: number, data: DeepPartial<User>): Promise<boolean> {
|
||||
const userRepository = this.dataSource.getRepository(User);
|
||||
const updateResult = await userRepository.update({ id: Number(userId) }, data);
|
||||
assert(updateResult.affected);
|
||||
|
||||
return updateResult.affected > 0;
|
||||
}
|
||||
|
||||
async getOrganizationsByUserId (userId: number): Promise<Organization[]> {
|
||||
const userOrganizationRepository = this.dataSource.getRepository(UserOrganization);
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@ export class User {
|
||||
@Column()
|
||||
email!: string;
|
||||
|
||||
@Column('varchar', { nullable: true })
|
||||
gitHubToken!: string | null;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date;
|
||||
|
||||
|
||||
@@ -245,15 +245,24 @@ export const createResolvers = async (db: Database, app: OAuthApp): Promise<any>
|
||||
}
|
||||
},
|
||||
|
||||
authenticateGithub: async (_: any, { code }: { code: string }) => {
|
||||
authenticateGitHub: async (_: any, { code }: { code: string }, context: any) => {
|
||||
// TOO: Move to Service class
|
||||
const { authentication: { token } } = await app.createToken({
|
||||
code
|
||||
});
|
||||
|
||||
// TODO: Save bearer token in DB for user
|
||||
await db.updateUser(context.userId, { gitHubToken: token });
|
||||
|
||||
return { token };
|
||||
},
|
||||
|
||||
unauthenticateGitHub: async (_: any, __: object, context: any) => {
|
||||
try {
|
||||
return db.updateUser(context.userId, { gitHubToken: null });
|
||||
} catch (err) {
|
||||
log(err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -34,6 +34,7 @@ type User {
|
||||
projects: [Project!]
|
||||
createdAt: String!
|
||||
updatedAt: String!
|
||||
gitHubToken: String
|
||||
}
|
||||
|
||||
type Organization {
|
||||
@@ -143,7 +144,8 @@ type Mutation {
|
||||
rollbackDeployment(projectId: String!, deploymentId: String!): Boolean!
|
||||
addDomain(projectId: String!, domainDetails: AddDomainInput!): Boolean!
|
||||
updateDomain(domainId: String!, domainDetails: UpdateDomainInput!): Boolean!
|
||||
authenticateGithub(code: String!): AuthResult!
|
||||
authenticateGitHub(code: String!): AuthResult!
|
||||
unauthenticateGitHub: Boolean!
|
||||
}
|
||||
|
||||
input AddEnvironmentVariableInput {
|
||||
|
||||
Reference in New Issue
Block a user