Skip to content

Commit

Permalink
Merge pull request #20 from Nabhag8848/OAuth2/logout
Browse files Browse the repository at this point in the history
[FEAT] `/notion logout`
  • Loading branch information
Nabhag8848 authored Mar 19, 2023
2 parents e41ef08 + da88886 commit ee95562
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions oauth2/commands/NotionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class NotionCommand implements ISlashCommand {
break;
}
case CmdParameters.LOGOUT: {
oauth2Instance.logout(read, http, modify, persis, context);
break;
}
case CmdParameters.TEST: {
Expand Down
41 changes: 37 additions & 4 deletions oauth2/lib/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,41 @@ export class OAuth2Client {
}
}

public async logout() {
return;
public async logout(
read: IRead,
http: IHttp,
modify: IModify,
persis: IPersistence,
context: SlashCommandContext
) {
const oAuthStorage = new OAuth2Storage(
persis,
read.getPersistenceReader()
);

const tokenInfo = await oAuthStorage.getTokenInfoOfUser(
context.getSender().id
);

const appBot = (await read.getUserReader().getAppUser()) as IUser;
const messageBuilder = modify
.getCreator()
.startMessage()
.setRoom(context.getRoom())
.setSender(appBot);

if (tokenInfo?.access_token) {
const tokenInfo = await oAuthStorage.removeTokenInfoOfUser(context.getSender().id);
messageBuilder.setText("✅ Logout Successful");
} else {
messageBuilder.setText(
`You are already logout! Login to your workspace \`/notion login\``
);
}

await modify
.getNotifier()
.notifyUser(context.getSender(), messageBuilder.getMessage());
}

public async test(
Expand All @@ -82,7 +115,7 @@ export class OAuth2Client {
persis,
read.getPersistenceReader()
);

const tokenInfo = await oAuthStorage.getTokenInfoOfUser(
context.getSender().id
);
Expand All @@ -92,7 +125,7 @@ export class OAuth2Client {
.startMessage()
.setRoom(context.getRoom())
.setSender(appBot);

if (tokenInfo?.access_token) {
messageBuilder.setText("User is already loggedIn :rocket:");
} else {
Expand Down
17 changes: 17 additions & 0 deletions oauth2/persistance/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ export class OAuth2Storage {

return tokenInfo;
}

public async removeTokenInfoOfUser(userId: string) {
const [removedTokenInfo] = (await this.persistence.removeByAssociations(
[
new RocketChatAssociationRecord( // user association
RocketChatAssociationModel.USER,
userId
),
new RocketChatAssociationRecord(
RocketChatAssociationModel.MISC, // access_info association
"access_token with info"
),
]
)) as ITokenInfo[];

return removedTokenInfo;
}
}

0 comments on commit ee95562

Please sign in to comment.