Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option for setting redirect_url for MagicLink sign in #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@ public Task<Session> SignInWithEmail(string email, string password)
/// Sends a magic login link to an email address.
/// </summary>
/// <param name="email"></param>
/// <param name="options"></param>
/// <returns></returns>
public Task<BaseResponse> SendMagicLinkEmail(string email)
public Task<BaseResponse> SendMagicLinkEmail(string email, SignInOptions options = null)
{
var data = new Dictionary<string, string> { { "email", email } };
if (options != null)
{
if (!string.IsNullOrEmpty(options.RedirectTo))
{
data.Add("redirect_to", options.RedirectTo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need to uri encode the url at this point or if its handled by HttpUtility in Helpers.MakeRequest.

When we have the tests working, we can test this.

}
}

return Helpers.MakeRequest(HttpMethod.Post, $"{Url}/magiclink", data, Headers);
}

Expand Down Expand Up @@ -377,16 +386,22 @@ public Task<Session> RefreshAccessToken(string refreshToken)
/// <summary>
/// Options used for signing up a user.
/// </summary>
public class SignUpOptions
public class SignUpOptions : SignInOptions
{
/// <summary>
/// A URL or mobile address to send the user to after they are confirmed.
/// Optional user metadata.
/// </summary>
public string RedirectTo { get; set; }
public Dictionary<string, object> Data { get; set; }
}

// <summary>
/// Options used for signing in a user.
/// </summary>
public class SignInOptions
{
/// <summary>
/// Optional user metadata.
/// A URL or mobile address to send the user to after they are confirmed.
/// </summary>
public Dictionary<string, object> Data { get; set; }
public string RedirectTo { get; set; }
}
}
5 changes: 3 additions & 2 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,15 @@ public async Task<Session> SignUp(SignUpType type, string identifier, string pas
/// Sends a Magic email login link to the specified email.
/// </summary>
/// <param name="email"></param>
/// <param name="options"></param>
/// <returns></returns>
public async Task<bool> SignIn(string email)
public async Task<bool> SignIn(string email, SignInOptions options = null)
{
await DestroySession();

try
{
await api.SendMagicLinkEmail(email);
await api.SendMagicLinkEmail(email, options);
return true;
}
catch (RequestException ex)
Expand Down
6 changes: 4 additions & 2 deletions Gotrue/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ public static async Task<Session> SignUp(SignUpType type, string identifier, str
/// Sends a Magic email login link to the specified email.
/// </summary>
/// <param name="email"></param>
/// <param name="options"></param>
/// <param name="signInOptions"></param>
/// <returns></returns>
public static async Task<bool> SignIn(string email, StatelessClientOptions options)
public static async Task<bool> SignIn(string email, StatelessClientOptions options, SignInOptions signInOptions = null)
{
try
{
await GetApi(options).SendMagicLinkEmail(email);
await GetApi(options).SendMagicLinkEmail(email, signInOptions);
return true;
}
catch (RequestException ex)
Expand Down
24 changes: 16 additions & 8 deletions GotrueTests/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,29 @@ public async Task SignsOut()
[TestMethod("StatelessClient: Sends Magic Login Email")]
public async Task SendsMagicLoginEmail()
{
var user = $"{RandomString(12)}@supabase.io";
await SignUp(user, password, options);
var user1 = $"{RandomString(12)}@supabase.io";
await SignUp(user1, password, options);

var result = await SignIn(user, options);
Assert.IsTrue(result);
var result1 = await SignIn(user1, options);
Assert.IsTrue(result1);

var user2 = $"{RandomString(12)}@supabase.io";
var result2 = await SignIn(user2, options, new SignInOptions { RedirectTo = $"com.{RandomString(12)}.deeplink://login" });
Assert.IsTrue(result2);
}

[TestMethod("StatelessClient: Sends Magic Login Email (Alias)")]
public async Task SendsMagicLoginEmailAlias()
{
var user = $"{RandomString(12)}@supabase.io";
await SignUp(user, password, options);
var user1 = $"{RandomString(12)}@supabase.io";
await SignUp(user1, password, options);

var result = await SendMagicLink(user, options);
Assert.IsTrue(result);
var result1 = await SignIn(user1, options);
Assert.IsTrue(result1);

var user2 = $"{RandomString(12)}@supabase.io";
var result2 = await SignIn(user2, options, new SignInOptions { RedirectTo = $"com.{RandomString(12)}.deeplink://login" });
Assert.IsTrue(result2);
}

[TestMethod("StatelessClient: Returns Auth Url for Provider")]
Expand Down