Skip to content

Commit

Permalink
Implements #41 - Add support for VerifyOTP(string email, string token)
Browse files Browse the repository at this point in the history
  • Loading branch information
acupofjose committed Nov 1, 2022
1 parent 6182de9 commit f45d47c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
20 changes: 18 additions & 2 deletions Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,28 @@ public Task<BaseResponse> SendMobileOTP(string phone)
/// <param name="phone">The user's phone number WITH international prefix</param>
/// <param name="token">token that user was sent to their mobile phone</param>
/// <returns></returns>
public Task<Session> VerifyMobileOTP(string phone, string token)
public Task<Session> VerifyMobileOTP(string phone, string token, MobileOtpType type)
{
var data = new Dictionary<string, string> {
{ "phone", phone },
{ "token", token },
{ "type", "sms" }
{ "type", Helpers.GetMappedToAttr(type).Mapping }
};
return Helpers.MakeRequest<Session>(HttpMethod.Post, $"{Url}/verify", data, Headers);
}

/// <summary>
/// Send User supplied Mobile OTP to be verified
/// </summary>
/// <param name="phone">The user's phone number WITH international prefix</param>
/// <param name="token">token that user was sent to their mobile phone</param>
/// <returns></returns>
public Task<Session> VerifyEmailOTP(string email, string token, EmailOtpType type)
{
var data = new Dictionary<string, string> {
{ "email", email },
{ "token", token },
{ "type", Helpers.GetMappedToAttr(type).Mapping }
};
return Helpers.MakeRequest<Session>(HttpMethod.Post, $"{Url}/verify", data, Headers);
}
Expand Down
34 changes: 32 additions & 2 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,43 @@ public async Task<string> SignIn(Provider provider, string scopes = null)
/// <param name="phone">The user's phone number.</param>
/// <param name="token">Token sent to the user's phone.</param>
/// <returns></returns>
public async Task<Session> VerifyOTP(string phone, string token)
public async Task<Session> VerifyOTP(string phone, string token, MobileOtpType type = MobileOtpType.SMS)
{
try
{
await DestroySession();

var session = await api.VerifyMobileOTP(phone, token);
var session = await api.VerifyMobileOTP(phone, token, type);

if (session?.AccessToken != null)
{
await PersistSession(session);
StateChanged?.Invoke(this, new ClientStateChanged(AuthState.SignedIn));
return session;
}

return null;
}
catch (RequestException ex)
{
throw ExceptionHandler.Parse(ex);
}
}

/// <summary>
/// Log in a user give a user supplied OTP received via email.
/// </summary>
/// <param name="email"></param>
/// <param name="token"></param>
/// <param name="type"></param>
/// <returns></returns>
public async Task<Session> VerifyOTP(string email, string token, EmailOtpType type = EmailOtpType.MagicLink)
{
try
{
await DestroySession();

var session = await api.VerifyEmailOTP(email, token, type);

if (session?.AccessToken != null)
{
Expand Down
22 changes: 22 additions & 0 deletions Gotrue/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,27 @@ public enum SortOrder
[MapTo("desc")]
Descending
}

public enum MobileOtpType
{
[MapTo("sms")]
SMS,
[MapTo("phone_change")]
PhoneChange
}

public enum EmailOtpType
{
[MapTo("signup")]
Signup,
[MapTo("invite")]
Invite,
[MapTo("magiclink")]
MagicLink,
[MapTo("recovery")]
Recovery,
[MapTo("email_change")]
EmailChange
}
}
}
31 changes: 29 additions & 2 deletions Gotrue/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ public static async Task<bool> SignOut(string jwt, StatelessClientOptions option
/// <param name="phone">The user's phone number.</param>
/// <param name="token">Token sent to the user's phone.</param>
/// <returns></returns>
public static async Task<Session> VerifyOTP(string phone, string token, StatelessClientOptions options)
public static async Task<Session> VerifyOTP(string phone, string token, StatelessClientOptions options, MobileOtpType type = MobileOtpType.SMS)
{
try
{
var session = await GetApi(options).VerifyMobileOTP(phone, token);
var session = await GetApi(options).VerifyMobileOTP(phone, token, type);

if (session?.AccessToken != null)
{
Expand All @@ -223,6 +223,33 @@ public static async Task<Session> VerifyOTP(string phone, string token, Stateles
}
}

/// <summary>
/// Log in a user give a user supplied OTP received via email.
/// </summary>
/// <param name="email"></param>
/// <param name="token"></param>
/// <param name="type"></param>
/// <returns></returns>
public static async Task<Session> VerifyOTP(string email, string token, StatelessClientOptions options, EmailOtpType type = EmailOtpType.MagicLink)
{
try
{
var session = await GetApi(options).VerifyEmailOTP(email, token, type);

if (session?.AccessToken != null)
{
return session;
}

return null;
}
catch (RequestException ex)
{
throw ExceptionHandler.Parse(ex);
}
}


/// <summary>
/// Updates a User.
/// </summary>
Expand Down

0 comments on commit f45d47c

Please sign in to comment.