# 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.

```dart
await MBAuth.authenticateUser('email', 'password');
```

## Social

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

Socials currently supported:

* Google
* Facebook
* Apple

```dart
await MBAuth.authenticateUserWithSocial(
      'socialToken',
      MBAuthSocialLoginType.facebook,
    );
```

{% 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.userLoggedIn()`**.

If a user is authenticated you can retrieve its access token with **`MBAuth.userToken()`** else this will return **`null`**.

To logout the current user:

```dart
await MBAuth.logoutCurrentUser();
```

**`MBAuth`** saves the user information using the [flutter\_secure\_storage](https://pub.dev/packages/flutter_secure_storage) package.&#x20;
