StitchAuth
public protocol StitchAuth
The StitchAuth
provides methods for retrieving or modifying the authentication
state of a StitchAppClient
.
Each StitchAppClient
has an instance of StitchAuth.
Information about the logged-in StitchUser
is available in the currentUser
property.
To watch for auth events, add a StitchAuthDelegate
.
-
Whether or not the client containing this
StitchAuth
object is currently authenticated.Declaration
Swift
var isLoggedIn: Bool { get }
-
A
StitchUser
object representing the user that the client is currently authenticated as.nil
if the client is not currently authenticated.Declaration
Swift
var currentUser: StitchUser? { get }
-
Retrieves the authenticated authentication provider client for the authentication provider associated with the specified factory.
Declaration
Swift
func providerClient<Factory>(fromFactory factory: Factory) -> Factory.ClientT where Factory : AuthProviderClientFactory, Factory.RequestClientT == StitchAuthRequestClient
Parameters
fromFactory
The
AuthProviderClientFactory
which will provide the client for this authentication provider. Each authentication provider that has extra functionality beyond logging in/linking will offer a static factory which can be used for this method.Return Value
an authentication provider client whose type is determined by the
Client
typealias in the type specified in thefromFactory
parameter. -
Retrieves the authentication provider client for the authentication provider associated with the specified factory.
Declaration
Swift
func providerClient<Factory>(fromFactory factory: Factory) -> Factory.ClientT where Factory : AuthProviderClientFactory, Factory.RequestClientT == StitchRequestClient
Parameters
fromFactory
The
AuthProviderClientFactory
which will provide the client for this authentication provider. Each authentication provider that has extra functionality beyond logging in/linking will offer a static factory which can be used for this method.Return Value
an authentication provider client whose type is determined by the
Client
typealias in the type specified in thefromFactory
parameter. -
Retrieves the authenticated authentication provider client for the authentication provider associated with the specified name and factory.
Declaration
Swift
func providerClient<Factory>(fromFactory factory: Factory, withName name: String) -> Factory.Client where Factory : NamedAuthProviderClientFactory
Parameters
fromFactory
The
NamedAuthProviderClientFactory
which will provide the client for this authentication provider. Each named authentication provider that has extra functionality beyond logging in/linking will offer a static factory which can be used for this method.withName
The name of the authentication provider as defined in the MongoDB Stitch application.
Return Value
an authentication provider client whose type is determined by the
Client
typealias in the type specified in thefromFactory
parameter.
-
Authenticates the client as a MongoDB Stitch user using the provided
StitchCredential
. On success, this user will become the active user.Declaration
Swift
func login(withCredential credential: StitchCredential, _ completionHandler: @escaping (StitchResult<StitchUser>) -> Void)
Parameters
withCredential
The
StitchCredential
used to authenticate the client. Credentials can be retrieved from an authentication provider client, which is retrieved using theproviderClient
method.completionHandler
The completion handler to call when the login is complete. This handler is executed on a non-main global
DispatchQueue
. If the operation is successful, the result will contain aStitchUser
object representing the user that the client is now authenticated as. -
Logs out the currently authenticated user, and clears any persisted authentication information.
Declaration
Swift
func logout(_ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
completionHandler
The completion handler to call when the logout is complete. This handler is executed on a non-main global
DispatchQueue
. -
Logs out of the user with the given userId. The user must exist in the list of all users who have logged into this application otherwise this will throw a StitchServiveError.
Declaration
Swift
func logoutUser(withId userId: String, _ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
userId
A String specifying the desired
userId
completionHandler
The completion handler to call when the switch is complete. This handler is executed on a non-main global
DispatchQueue
. -
Switches the active user to the user with the specified id. The user must exist in the list of all users who have logged into this application, and the user must be currently logged in, otherwise this will throw a StitchServiveError.
Declaration
Swift
func switchToUser(withId userId: String) throws -> StitchUser
Parameters
userId
A String specifying the desired
userId
-
Removes the current active user from the list of all users associated with this application. If there is no currently active user, then the function will return with success. Additionally, this method will clear all user data including any synchronized databases.
Declaration
Swift
func removeUser(_ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
completionHandler
The completion handler to call when the switch is complete. This handler is executed on a non-main global
DispatchQueue
. -
Removes the user with the provided id from the list of all users associated with this application. If the user was logged in, the user will be logged out before being removed. The user must exist in the list of all users who have logged into this application otherwise this will throw a StitchServiveError. Additionally, this method will clear all user data including any synchronized databases.
Declaration
Swift
func removeUser(withId userId: String, _ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
userId
A String specifying the desired
userId
completionHandler
The completion handler to call when the switch is complete. This handler is executed on a non-main global
DispatchQueue
. -
Returns a list of all users who have logged into this application, with the exception of those that have been removed manually and anonymous users who have logged out. This list is guaranteed to be in the order that the users were added to the application.
Declaration
Swift
func listUsers() -> [StitchUser]
Parameters
completionHandler
The completion handler to call when the switch is complete. This handler is executed on a non-main global
DispatchQueue
.
-
Registers a
StitchAuthDelegate
with the client. TheStitchAuthDelegate
‘sonAuthEvent(:fromAuth)
method will be called with thisStitchAuth
as the argument whenever this client is authenticated or is logged out.Important
StitchAuthDelegates registered here are stored asweak
references, meaning that if there are no more strong references to a provided delegate, itsonAuthEvent(:fromAuth)
method will no longer be called on authentication events.Declaration
Swift
func add(authDelegate: StitchAuthDelegate)
Parameters
authDelegate
A class conforming to
StitchAuthDelegate
, whoseonAuthEvent(:fromAuth)
method should be called whenever this client experiences an authentication event.