microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6a465861663d7c6f9433e89c6f6619df320dcef3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/appcenter/auth/auth.ts

70lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import { SettingsHelper } from "../../../extension/settingsHelper";
6import { createAppCenterClient, getQPromisifiedClientResult } from "../api/index";
7import { Profile, saveUser, deleteUser, getUser } from "../auth/profile/profile";
8import * as models from "app-center-node-client/models";
9
10export default class Auth {
11 public static isAuthenticated(): Q.Promise<boolean> {
12 const currentUser = getUser();
13 if (currentUser) {
14 return Q<boolean>(true);
15 } else {
16 return Q<boolean>(false);
17 }
18 }
19
20 public static whoAmI(): Q.Promise<string> {
21 const currentUser = getUser();
22 if (currentUser) {
23 return Q.resolve(currentUser.displayName);
24 } else {
25 return Q.resolve("");
26 }
27 }
28
29 public static doTokenLogin(token: string): Q.Promise<Profile | null> {
30 if (!token) {
31 return Q.resolve(null);
32 }
33 return this.removeLoggedInUser().then(() => {
34 return Auth.fetchUserInfoByTokenAndSave(token).then((profile: Profile) => {
35 return Q.resolve(profile);
36 });
37 });
38 }
39
40 public static doLogout(): Q.Promise<void> {
41 // TODO: Probably we need to delete token from server also?
42 return this.removeLoggedInUser();
43 }
44
45 private static fetchUserInfoByTokenAndSave(token: string): Q.Promise<Profile> {
46 return Auth.getUserInfo(token).then(userResponse => {
47 return saveUser(userResponse, { token: token }).then((profile: Profile) => {
48 return Q.resolve(profile);
49 });
50 }).catch((e: any) => {
51 throw e;
52 });
53 }
54
55 private static getUserInfo(token: string): Q.Promise<models.UserProfileResponse> {
56 const client = createAppCenterClient().fromToken(token, SettingsHelper.getAppCenterAPIEndpoint());
57 return getQPromisifiedClientResult(client.account.users.get());
58 }
59
60 private static removeLoggedInUser(): Q.Promise<void> {
61 const currentUser = getUser();
62 if (currentUser) {
63 // Deleting user token from token store
64 return deleteUser().then(() => {
65 return Q.resolve(void 0);
66 }).catch(() => { }); // Noop, it's ok if deletion fails
67 }
68 return Q.resolve(void 0);
69 }
70}