Skip to content

Commit

Permalink
Merge pull request #173 from YkSix/feature/initial-qr-screen
Browse files Browse the repository at this point in the history
Add a parameter for setting initialWebAuthenticationMethod
  • Loading branch information
YkSix authored Dec 27, 2024
2 parents c0759a5 + 4e24c10 commit 1b28843
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.linecorp.linesdk.sample.ui.homeScreen

import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -31,6 +38,9 @@ fun LoginButtonGroup(
loginDelegateForLineLoginBtn: LoginDelegate,
onSimpleLoginButtonPressed: (Intent) -> Unit
) {
var forceWebLogin by rememberSaveable { mutableStateOf(false) }
var qrCodeLogin by rememberSaveable { mutableStateOf(false) }

LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
Expand All @@ -57,32 +67,32 @@ fun LoginButtonGroup(
ApiDemoButton(
"login",
modifier = Modifier
.fillMaxWidth(),
enabled = !isLogin
) {
val intent = loginViewModel.createLoginIntent(
context,
channelId,
scopeList
)
onSimpleLoginButtonPressed(intent)
}

ApiDemoButton(
"web login",
modifier = Modifier
.fillMaxWidth(),
.fillMaxWidth()
.padding(top = 24.dp),
enabled = !isLogin
) {
val intent = loginViewModel.createLoginIntent(
context,
channelId,
scopeList,
onlyWebLogin = true
forceWebLogin = forceWebLogin,
qrCodeLogin = qrCodeLogin
)
onSimpleLoginButtonPressed(intent)
}

LabeledCheckbox(
label = "Force web login",
checked = forceWebLogin,
onCheckedChange = { forceWebLogin = it }
)

LabeledCheckbox(
label = "QR code login",
checked = qrCodeLogin,
onCheckedChange = { qrCodeLogin = it }
)

ApiDemoButton(
"logout",
modifier = Modifier
Expand All @@ -104,3 +114,19 @@ fun LoginButtonGroup(
}
}
}

@Composable
fun LabeledCheckbox(label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable { onCheckedChange(!checked) }
) {
Checkbox(
checked = checked,
onCheckedChange = onCheckedChange
)
Text(text = label)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import androidx.lifecycle.viewModelScope
import com.linecorp.linesdk.LineProfile
import com.linecorp.linesdk.Scope
import com.linecorp.linesdk.auth.LineAuthenticationParams
import com.linecorp.linesdk.auth.LineAuthenticationParams.WebAuthenticationMethod
import com.linecorp.linesdk.auth.LineAuthenticationParams.WebAuthenticationMethod.email
import com.linecorp.linesdk.auth.LineAuthenticationParams.WebAuthenticationMethod.qrCode
import com.linecorp.linesdk.auth.LineLoginApi
import com.linecorp.linesdk.auth.LineLoginResult
import java.util.Locale
Expand Down Expand Up @@ -46,11 +49,18 @@ class LoginViewModel(context: Context, channelId: String) :
nonce: String? = null,
botPrompt: LineAuthenticationParams.BotPrompt? = null,
uiLocale: Locale? = null,
onlyWebLogin: Boolean = false
forceWebLogin: Boolean = false,
qrCodeLogin: Boolean = false
): Intent {
val loginAuthParam = createLoginAuthParam(scopes, nonce, botPrompt, uiLocale)

return if (onlyWebLogin) {
val loginAuthParam = createLoginAuthParam(
scopes,
nonce,
botPrompt,
uiLocale,
webAuthMethod = if (qrCodeLogin) qrCode else email
)

return if (forceWebLogin) {
LineLoginApi.getLoginIntentWithoutLineAppAuth(
context,
channelId,
Expand Down Expand Up @@ -119,12 +129,14 @@ class LoginViewModel(context: Context, channelId: String) :
scopes: List<Scope>,
nonce: String?,
botPrompt: LineAuthenticationParams.BotPrompt?,
uiLocale: Locale?
uiLocale: Locale?,
webAuthMethod: WebAuthenticationMethod
): LineAuthenticationParams = LineAuthenticationParams.Builder()
.scopes(scopes)
.nonce(nonce)
.uiLocale(uiLocale)
.botPrompt(botPrompt)
.initialWebAuthenticationMethod(webAuthMethod)
.build()

private fun fetchAndUpdateUserProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ public LineAuthenticationParams[] newArray(final int size) {
@Nullable
private final String promptBotID;

/**
* OPTIONAL. <br></br>
* The initial web authentication method to be used when starting the login process.
*/
@Nullable
private final WebAuthenticationMethod webAuthMethod;

private LineAuthenticationParams(final Builder builder) {
scopes = builder.scopes;
nonce = builder.nonce;
botPrompt = builder.botPrompt;
uiLocale = builder.uiLocale;
promptBotID = builder.promptBotID;
webAuthMethod = builder.webAuthMethod;
}

private LineAuthenticationParams(@NonNull final Parcel in) {
Expand All @@ -84,6 +92,7 @@ private LineAuthenticationParams(@NonNull final Parcel in) {
botPrompt = readEnum(in, BotPrompt.class);
uiLocale = (Locale) in.readSerializable();
promptBotID = in.readString();
webAuthMethod = readEnum(in, WebAuthenticationMethod.class);
}

/**
Expand All @@ -98,6 +107,7 @@ public void writeToParcel(final Parcel dest, final int flags) {
writeEnum(dest, botPrompt);
dest.writeSerializable(uiLocale);
dest.writeString(promptBotID);
writeEnum(dest, webAuthMethod);
}

/**
Expand Down Expand Up @@ -155,6 +165,11 @@ public String getPromptBotID() {
return promptBotID;
}

@Nullable
public WebAuthenticationMethod getInitialWebAuthenticationMethod() {
return webAuthMethod;
}

/**
* Represents an option to determine how to prompt the user to add a LINE Official Account
* as a friend during the login process.
Expand All @@ -172,6 +187,14 @@ public enum BotPrompt {
aggressive
}

/**
* The method used for the authentication when using the web authentication flow.
*/
public enum WebAuthenticationMethod {
email,
qrCode
}

/**
* Represents a builder to construct LineAuthenticationParams objects.
*/
Expand All @@ -180,8 +203,8 @@ public static final class Builder {
private String nonce;
private BotPrompt botPrompt;
private Locale uiLocale;

private String promptBotID;
private WebAuthenticationMethod webAuthMethod;

public Builder() {}

Expand Down Expand Up @@ -232,6 +255,14 @@ public Builder promptBotID(final String botID) {
return this;
}

/**
* Specifies the initial web authentication method to be used when starting the login process.
*/
public Builder initialWebAuthenticationMethod(final WebAuthenticationMethod method) {
webAuthMethod = method;
return this;
}

/**
* Builds LineAuthenticationParams objects.
* @return The LineAuthenticationParams object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.linecorp.linesdk.Scope;
import com.linecorp.linesdk.auth.LineAuthenticationConfig;
import com.linecorp.linesdk.auth.LineAuthenticationParams;
import com.linecorp.linesdk.auth.LineAuthenticationParams.WebAuthenticationMethod;
import com.linecorp.linesdk.internal.pkce.CodeChallengeMethod;
import com.linecorp.linesdk.internal.pkce.PKCECode;

Expand Down Expand Up @@ -180,7 +181,17 @@ Uri createLoginUrl(
if (params.getUILocale() != null) {
loginQueryParams.put("ui_locales", params.getUILocale().toString());
}
return appendQueryParams(config.getWebLoginPageUrl(), loginQueryParams);

Uri loginPageUrl = config.getWebLoginPageUrl();
Uri.Builder urlBuilder = loginPageUrl.buildUpon();
if (params.getInitialWebAuthenticationMethod() == WebAuthenticationMethod.qrCode) {
if (loginPageUrl.getFragment() != null) {
throw new AssertionError("Multiple fragment is not yet supported. Require review or report to developer.");
}
urlBuilder.encodedFragment("/qr");
}

return appendQueryParams(urlBuilder, loginQueryParams).build();
}

@VisibleForTesting
Expand Down

0 comments on commit 1b28843

Please sign in to comment.