# Authenticate a user

## Email and password

After registering the user, you can authenticate it with its email and password.&#x20;

{% hint style="info" %}
All the communication with the server is made in https, so all the data is encrypted.&#x20;

If the authentication is correct, the api will return the access token.&#x20;
{% endhint %}

This token will be put in the **`Authorization`** header for each subsequent call to all the MBurger apis.

```swift
MBAuth.authenticateUser(withEmail: "email", password: "password", success: { accessToken in
           
}, failure: { error in

})
```

## Social

MBurger offers the possibility to authenticate a user with social networks too.

Socials currently supported:

* Google
* Facebook
* Apple

```swift
MBAuth.authenticateUser(withSocialToken: "socialToken", 
                        tokenType: Social_Token_Type, 
                        success: { accessToken in
}, failure: { error in
})
```

{% hint style="info" %}
If the user logs in with apple you need to pass to this function also the name and surname because those cannot be retrieved by the server&#x20;
{% endhint %}

## How to know if user is logged in

You can see if a user is currently authenticated with **`MBAuth.userIsLoggedIn`**.

If a user is authenticated you can retrieve its access token with **`MBAuth.authToken`** else this will return **`nil`**.

To logout the current user:

```swift
MBAuth.logoutCurrentUser({ 
}, failure: { error in
})
```

**`MBAuth`** saves the user information in the Keychain.&#x20;

{% hint style="info" %}
If you are having issues when authenticating a user with the message “Couldn’t add the Keychain Item.” turn on Keychain sharing in your app capabilities section n for your app and add “com.mumble.mburger”. This should fix it.
{% endhint %}

![Keychain settings](https://docs.mumbleideas.it/mburger/ios/images/Keychain.png)

### &#x20;<a href="#retrieve-user-information" id="retrieve-user-information"></a>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mburger.cloud/ios-docs/auth/authenticate-a-user.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
