MGT Login control in SPFx
Tags: microsoftgraphtoolkit, spfx
Published: 2024-03-27 11:39:52

---
title: MGT Login control in SPFx
published: true
date: 2024-03-27 10:00:00 UTC
tags: MicrosoftGraphToolkit,spfx
canonical_url: https://iamguidozam.blog/2024/03/27/mgt-login-control-in-spfx/
---

Proceeding with the appointments with the MGT (Microsoft Graph Toolkit) controls today I want to talk about the **File List** control.

* * *

_I will not cover in detail the implementation, it’s not the scope of this post, if you’re wondering how to achieve all the steps to enable you to use MGT inside SPFx you can have a look at my [previous post](https://iamguidozam.blog/2023/12/20/use-microsoft-graph-toolkit-with-spfx-and-react/) or have a look at the code of this [sample here](https://github.com/GuidoZam/blog-samples/tree/main/MGT/mgt-login-spfx)._

* * *

The **Login** control is used to enable the user to login/logout, to display the currently logged in user and also to display more info and with the control templates you can also display custom controls or informations.

In the sample I created for this post you can see the login button for each control instance I created in the page:

![](https://iamguidozamcode.files.wordpress.com/2024/02/image-29.png?w=555)

The controls display an already disabled sign in button because the login process start almost immediately, otherwise you would see a clickable button.

Once the control completed the sign in process you would see something like the following:

![](https://iamguidozamcode.files.wordpress.com/2024/02/image-30.png?w=967)

![](https://iamguidozamcode.files.wordpress.com/2024/02/image-31.png?w=980)

In the picture above I bet you already noticed that there are two controls with a different visualization, one is the ninja one which has only custom data and does not rely on the currently logged in user, aside of that there is also the “Full customization” one, which you will see in the code chapter, has every aspect customized as you can see from the picture.

By default the control offer also a flyout card when clicked:

![](https://iamguidozamcode.files.wordpress.com/2024/02/image-32.png?w=459)

As said before you can customize the control, in the following image I customized the flyout commands allowing a dummy “add friend” and “send message” buttons:

![](https://iamguidozamcode.files.wordpress.com/2024/02/image-33.png?w=459)

#### The code

To use the **Login** control you have to import it like the following:

```
import { Login, MgtTemplateProps } from '@microsoft/mgt-react';
```

**NB** : In this sample the **MgtTemplateProps** is used only for the custom template, if you’re not planning to define a custom template you can avoid importing the class.

The simplest way to use the control is:

```
<Login />
```

Other ways to use the control, as you can see from the pictures above and from the code sample, are the following.

Adding the presence to the avatar:

```
<Login showPresence={true} />
```

Using the loginView property you can specify which type of view you want to display:

```
<Login loginView='compact' />
```

The loginView property have three possible values and those are:

- avatar
- compact
- full

As said before you can define custom data to be displayed, for the ninja example I used the following:

```
<Login userDetails={{ displayName: 'I\'m a ninja!', department: 'Ninja Department', personImage: this._ninjaUserImage }}/>
```

Where the _\_ninjaUserImage_ is an image base64 string.

Other useful properties are the events that you can subscribe to and perform custom code, the available events are:

- _loginInitiated_
- _loginCompleted_
- _loginFailed_
- _logoutInitiated_
- _logoutCompleted_

##### Templating

MGT controls offer the ability to define a custom templates, for the **Login** control you can define it like the following:

```
<Login> <this.LoginTemplate template="signed-in-button-content"/> <this.LoginTemplate template="signed-out-button-content"/> <this.LoginTemplate template="flyout-commands"/> <this.LoginTemplate template="flyout-person-details"/></Login>
```

Where _this.LoginTemplate_ is a function that renders the customized control, if you’re interested in knowing more is better if you take a look at the sample code [here](https://github.com/GuidoZam/blog-samples/blob/3944abea9f27e7faa13acbde04f221a336dc9eb4/MGT/mgt-login-spfx/src/webparts/mgtLogin/components/MgtLogin.tsx#L109C3-L109C11).

#### Conclusion

The MGT **Login** control is a quick and easy way to let your users perform sign in and allow them to have a little bit more personalized user experience.

Hope this helps!