# Update user metadata after MFA completion

**URL:** <https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523>\
**Category:** Get Help\
**Tags:** policies-configuration, mfa\
**Created:** [March 14, 2023, 5:36pm UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523 "2023-03-14T17:36:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunter1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/hunter1/32/25743_2.png) [@hunter1](https://community.auth0.com/u/hunter1)\
**Post date:** [March 14, 2023, 5:36pm UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/1 "2023-03-14T17:36:15Z")

</div>

Hello,

I’m working on a custom Action to provide step-up MFA when a user logs in from a new device. This works by passing in the deviceId as a query parameter and when it doesnt match the deviceId in the user metadata, (or first time login) they are pushed into the MFA flow. Upon completion, their metadata is updated and can log in from the known devices stored in their user profile.

The issue I’m having is verifying that they completed MFA before the metadata updated. I’m trying to reference the event.authentication.methods object for this but the Action is executed before event.authentication can be updated. Is there a way to verify MFA has been completed in the login flow before completing some task?

See example below:

```auto
exports.onExecutePostLogin = async (event, api) => {
//Pull request_id and visitor_id from query string
let device = event.request.query.device
var array = device.split(" ");
const vid = array[0]
const rid = array[1]
//confirm vid and rid values

let metadata = event.user.app_metadata
//Capture device info in Auth0 user profile
//If user does not have an existing device_id

if(!metadata.device_id){
api.multifactor.enable('any')
}

//force MFA on users logging in from new device

if (metadata.device_id && !event.user.app_metadata.device_id.includes(vid)) {
api.multifactor.enable('any');
}

//verify successful MFA before updating record
if (event.authentication?.methods.find(x => x.name == 'mfa')) {

//Update metadata if MFA is successful
if (!metadata.device_id) {
api.user.setAppMetadata("device_id", [vid]);
console.log('metadata updated')

} else if (!metadata.device_id.includes(vid)) {
let newArr = metadata.device_id;
newArr.unshift(vid)
api.user.setAppMetadata("device_id",newArr );
console.log('MFA triggered based on new device')
}
}

};

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 14, 2023, 11:13pm UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/3 "2023-03-14T23:13:10Z")

</div>

Hi there @hunter1!

> [@hunter1](#):
>
> The issue I’m having is verifying that they completed MFA before the metadata updated. I’m trying to reference the event.authentication.methods object for this but the Action is executed before event.authentication can be updated. Is there a way to verify MFA has been completed in the login flow before completing some task?

Unfortunately as you’ve noticed MFA does not complete until _after_ Actions/Rules have executed - It’s outside the scope of an Action, but you could potentially look at ID token claims to see if MFA was performed, or add a claim to an access token to indicate that MFA was required for a given login and go about it that way. An ID token returned after a login requiring MFA will contain both the `acr` and `amr` claims:

 ![Screenshot 2023-03-14 at 4.12.47 PM](https://global.discourse-cdn.com/auth0/original/3X/b/b/bb1df2783f4ed701fc6c0138f859e0460ddbb950.png)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![ben9](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ben9/32/13799_2.png) [@ben9](https://community.auth0.com/u/ben9)\
**Post date:** [March 17, 2023, 5:23pm UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/4 "2023-03-17T17:23:51Z")

</div>

having the same need, thanks for the hints above.

Using rails + omniauth, `request.env["omniauth.auth"]` outputs the following :

```auto

 {"provider"=>"auth0",
"uid"=>"auth0|xxx",
"info"=>
 {"name"=>"auth0|xxx",
  "nickname"=>nil,
  "email"=>nil,
  "image"=>nil},
"credentials"=>
 {"token"=>
   "...",
  "expires_at"=>1679159975,
  "expires"=>true,
  "id_token"=>
   "...",
  "token_type"=>"Bearer",
  "refresh_token"=>nil},
"extra"=>
 {"raw_info"=>
   {"iss"=>"https://xxx-development.eu.auth0.com/",
    "aud"=>"q3-E7Xa6G7JgS8MLIAx_gry9VutMkBpN",
    "iat"=>1679073576,
    "exp"=>1679109576,
    "sub"=>"auth0|xxx",
    "auth_time"=>1679073575,
    "acr"=>"http://schemas.openid.net/pape/policies/2007/06/multi-factor",
    "amr"=>["mfa"],
    "sid"=>"xxx",
    "nonce"=>"xxx"}}}

```

( `request.env["omniauth.auth"]` will be available within your callback route)

As advertised in the previous comment, we can see acr / amr after an MFA authentification. There aren’t present after a standard auth via email/password

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 17, 2023, 10:27pm UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/5 "2023-03-17T22:27:03Z")

</div>

Hey there @ben9! That’s great, thanks for the input 🙂 I wanted to also add that using [log streams](https://auth0.com/docs/customize/log-streams) may also be an option.

Cheers!

---

<div class="post-metadata">

**Author:** ![jeremie.poisson](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jeremie.poisson/32/33711_2.png) [@jeremie.poisson](https://community.auth0.com/u/jeremie.poisson)\
**Post date:** [March 21, 2023, 9:07am UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/6 "2023-03-21T09:07:03Z")

</div>

Hi,

I’m looking for a similar behavior.

I’d like to update app\_metadata with the phone number that he user to enroll or verify MFA.

I’m not sure how it’s possible with what you’ve proposed.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [April 4, 2023, 9:07am UTC](https://community.auth0.com/t/update-user-metadata-after-mfa-completion/102523/7 "2023-04-04T09:07:58Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
