verifyUser method

  1. @override
Future<void> verifyUser(
  1. String jwt
)
override

Implementation

@override
Future<void> verifyUser(String jwt) async {
  //! here check if the verification jwt is saved on the db or not
  //! make a temp collection for users auth data like jwt verification
  //! or just save in the auth data collection

  // in here i should update the user from the database and make him verified
  var secretKey = app.authSettings.jwtSecretKey;
  var res = JWT.tryVerify(jwt, secretKey);
  if (res == null) {
    throw JwtEmailVerificationExpired();
  }
  var payload = res.payload;
  var id = payload[ModelFields.id];
  if (id is! String) {
    throw UserNotFoundToVerify();
  }

  var collection =
      dbService.mongoDbController.collection(app.authSettings.collectionName);
  //? first make sure that the saved jwt for the user is write
  var authData = await collection.doc(id).getData();
  if (authData == null) {
    throw FailedToVerifyException(1);
  }
  var savedJWT = authData[DbFields.verificationJWT];
  if (savedJWT == null) {
    throw FailedToVerifyException(4);
  }
  if (jwt != savedJWT) {
    throw FailedToVerifyException(5);
  }

  //? then delete the verification jwt form the auth data for the user
  var delete = modify.unset(DbFields.verificationJWT);
  var selector = where.eq(DBRKeys.id, id);
  var edit = await collection.updateOne(selector, delete);
  if (edit.failure) {
    throw FailedToVerifyException(2);
  }
  //? then mark the user as verified
  var writeRes = await collection.doc(id).update({DbFields.verified: true});
  if (writeRes.failure) {
    throw FailedToVerifyException(3);
  }
}