mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-08-14 20:40:18 +02:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
159fe0e376 |
@@ -24,7 +24,7 @@ namespace Streetwriters.Identity.Interfaces
|
|||||||
{
|
{
|
||||||
public interface ISMSSender
|
public interface ISMSSender
|
||||||
{
|
{
|
||||||
Task<string> SendOTPAsync(string number, IClient client);
|
Task<string?> SendOTPAsync(string number, IClient client);
|
||||||
Task<bool> VerifyOTPAsync(string id, string code);
|
Task<bool> VerifyOTPAsync(string id, string code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,6 +186,8 @@ namespace Streetwriters.Identity.Services
|
|||||||
ArgumentNullException.ThrowIfNull(form.PhoneNumber);
|
ArgumentNullException.ThrowIfNull(form.PhoneNumber);
|
||||||
await UserManager.SetPhoneNumberAsync(user, form.PhoneNumber);
|
await UserManager.SetPhoneNumberAsync(user, form.PhoneNumber);
|
||||||
var id = await SMSSender.SendOTPAsync(form.PhoneNumber, client);
|
var id = await SMSSender.SendOTPAsync(form.PhoneNumber, client);
|
||||||
|
if (string.IsNullOrEmpty(id)) throw new Exception("Failed to send SMS. Please try again.");
|
||||||
|
|
||||||
logger.LogInformation("SMS OTP sent for user: {UserId}, SMS ID: {SmsId}", user.Id, id);
|
logger.LogInformation("SMS OTP sent for user: {UserId}, SMS ID: {SmsId}", user.Id, id);
|
||||||
await this.ReplaceClaimAsync(user, MFAService.SMS_ID_CLAIM, id);
|
await this.ReplaceClaimAsync(user, MFAService.SMS_ID_CLAIM, id);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -23,36 +23,56 @@ using Streetwriters.Common;
|
|||||||
using Twilio.Rest.Verify.V2.Service;
|
using Twilio.Rest.Verify.V2.Service;
|
||||||
using Twilio;
|
using Twilio;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Streetwriters.Identity.Services
|
namespace Streetwriters.Identity.Services
|
||||||
{
|
{
|
||||||
public class SMSSender : ISMSSender
|
public class SMSSender : ISMSSender
|
||||||
{
|
{
|
||||||
public SMSSender()
|
private readonly ILogger<SMSSender> Logger;
|
||||||
|
public SMSSender(ILogger<SMSSender> logger)
|
||||||
{
|
{
|
||||||
|
Logger = logger;
|
||||||
if (!string.IsNullOrEmpty(Constants.TWILIO_ACCOUNT_SID) && !string.IsNullOrEmpty(Constants.TWILIO_AUTH_TOKEN))
|
if (!string.IsNullOrEmpty(Constants.TWILIO_ACCOUNT_SID) && !string.IsNullOrEmpty(Constants.TWILIO_AUTH_TOKEN))
|
||||||
{
|
{
|
||||||
TwilioClient.Init(Constants.TWILIO_ACCOUNT_SID, Constants.TWILIO_AUTH_TOKEN);
|
TwilioClient.Init(Constants.TWILIO_ACCOUNT_SID, Constants.TWILIO_AUTH_TOKEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> SendOTPAsync(string number, IClient app)
|
public async Task<string?> SendOTPAsync(string number, IClient app)
|
||||||
{
|
{
|
||||||
var verification = await VerificationResource.CreateAsync(
|
try
|
||||||
to: number,
|
{
|
||||||
channel: "sms",
|
var verification = await VerificationResource.CreateAsync(
|
||||||
pathServiceSid: Constants.TWILIO_SERVICE_SID
|
to: number,
|
||||||
);
|
channel: "sms",
|
||||||
return verification.Sid;
|
pathServiceSid: Constants.TWILIO_SERVICE_SID
|
||||||
|
);
|
||||||
|
return verification.Sid;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError(ex, "Error sending OTP with Twilio");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> VerifyOTPAsync(string id, string code)
|
public async Task<bool> VerifyOTPAsync(string id, string code)
|
||||||
{
|
{
|
||||||
return (await VerificationCheckResource.CreateAsync(
|
try
|
||||||
verificationSid: id,
|
{
|
||||||
pathServiceSid: Constants.TWILIO_SERVICE_SID,
|
return (await VerificationCheckResource.CreateAsync(
|
||||||
code: code
|
verificationSid: id,
|
||||||
)).Status == "approved";
|
pathServiceSid: Constants.TWILIO_SERVICE_SID,
|
||||||
|
code: code
|
||||||
|
)).Status == "approved";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError(ex, "Error verifying OTP with Twilio");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user