• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-js / 6007676806

29 Aug 2023 03:54AM UTC coverage: 85.258% (-0.05%) from 85.311%
6007676806

push

github

Kenneth Geisshirt
Skip custom auth function tests

881 of 1096 branches covered (0.0%)

Branch coverage included in aggregate %.

2294 of 2628 relevant lines covered (87.29%)

796.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.44
/packages/realm/src/app-services/Credentials.ts
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2022 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
import { App, assert, binding } from "../internal";
20

21
/**
22
 * Types of an authentication provider.
23
 */
24
export enum ProviderType {
1✔
25
  AnonUser = "anon-user",
1✔
26
  ApiKey = "api-key",
1✔
27
  LocalUserPass = "local-userpass",
1✔
28
  CustomFunction = "custom-function",
1✔
29
  CustomToken = "custom-token",
1✔
30
  OAuth2Google = "oauth2-google",
1✔
31
  OAuth2Facebook = "oauth2-facebook",
1✔
32
  OAuth2Apple = "oauth2-apple",
1✔
33
}
34

35
export function isProviderType(arg: string): arg is ProviderType {
36
  return Object.values(ProviderType).includes(arg as ProviderType);
1✔
37
}
38

39
export class Credentials {
40
  /** @internal */
41
  public internal: binding.AppCredentials;
42

43
  /** @internal */
44
  private constructor(internal: binding.AppCredentials) {
45
    this.internal = internal;
136✔
46
  }
47

48
  /**
49
   * Creates credentials for an anonymous user. These can only be used once - using them a second
50
   * time will result in a different user being logged in. If you need to get a user that has already logged
51
   * in with the Anonymous credentials, use {@link App.currentUser} or {@link App.allUsers}.
52
   * @param reuse - Reuse any existing anonymous user already logged in.
53
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
54
   * @see https://docs.mongodb.com/realm/authentication/anonymous/
55
   */
56
  static anonymous(reuse = true): Credentials {
101✔
57
    return new Credentials(binding.AppCredentials.anonymous(reuse));
101✔
58
  }
59

60
  /**
61
   * Creates credentials based on a login with an email address and a password.
62
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
63
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/email-password/
64
   */
65
  static emailPassword(credentials: { email: string; password: string }): Credentials;
66
  static emailPassword(email: string, password: string): Credentials;
67
  static emailPassword(arg1: { email: string; password: string } | string, password?: string): Credentials {
68
    if (typeof arg1 === "string") {
34✔
69
      assert.string(password, "password");
9✔
70
      return new Credentials(binding.AppCredentials.usernamePassword(arg1, password));
9✔
71
    } else {
72
      assert.string(arg1.email, "email");
25✔
73
      assert.string(arg1.password, "password");
24✔
74
      return new Credentials(binding.AppCredentials.usernamePassword(arg1.email, arg1.password));
23✔
75
    }
76
  }
77

78
  /**
79
   * Creates credentials from an API key.
80
   * @param key - A string identifying the API key.
81
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
82
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/
83
   */
84
  static apiKey(key: string): Credentials {
85
    return new Credentials(binding.AppCredentials.apiKey(key));
1✔
86
  }
87

88
  /**
89
   * Creates credentials based on an Apple login.
90
   * @param token - An Apple authentication token, obtained by logging into Apple.
91
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
92
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/apple/
93
   */
94
  static apple(token: string): Credentials {
95
    return new Credentials(binding.AppCredentials.apple(token));
×
96
  }
97

98
  /**
99
   * Creates credentials based on a Facebook login.
100
   * @param token - A Facebook authentication token, obtained by logging into Facebook.
101
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
102
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/facebook/
103
   */
104
  static facebook(token: string): Credentials {
105
    return new Credentials(binding.AppCredentials.facebook(token));
×
106
  }
107

108
  /**
109
   * Creates credentials based on a Google login.
110
   * @param authObject - An object with either an `authCode` or `idToken` property.
111
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
112
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/google/
113
   */
114
  static google(authObject: { authCode: string } | { idToken: string }): Credentials;
115
  static google({ authCode, idToken }: { authCode?: string; idToken?: string }): Credentials {
116
    let internal: binding.AppCredentials;
117
    if (authCode !== undefined) {
×
118
      assert(idToken === undefined, "Must not supply both an authCode or idToken field");
×
119
      internal = binding.AppCredentials.googleAuth(binding.GoogleAuthCode.make(authCode));
×
120
    } else {
121
      assert(idToken !== undefined, "Must supply either an authCode or idToken field");
×
122
      internal = binding.AppCredentials.googleId(binding.GoogleIdToken.make(idToken));
×
123
    }
124
    return new Credentials(internal);
×
125
  }
126

127
  /**
128
   * Creates credentials with a JSON Web Token (JWT) provider and user identifier.
129
   * @param token - A string identifying the user. Usually an identity token or a username.
130
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
131
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/custom-jwt/
132
   */
133
  static jwt(token: string): Credentials {
134
    return new Credentials(binding.AppCredentials.custom(token));
2✔
135
  }
136

137
  /**
138
   * Creates credentials with an Atlas App Services function and user identifier.
139
   * @param payload - An object identifying the user. Usually an identity token or a username.
140
   * @returns An instance of `Credentials` that can be used in {@link App.logIn}.
141
   * @see https://www.mongodb.com/docs/atlas/app-services/authentication/custom-function/
142
   */
143
  static function(payload: object): Credentials {
144
    return new Credentials(binding.AppCredentials.function(payload as Record<string, binding.EJson>));
×
145
  }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc