Skip to content

Instantly share code, notes, and snippets.

@seit
Last active December 21, 2022 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seit/65d562e958e5f0f285e9f6dadd1ea725 to your computer and use it in GitHub Desktop.
Save seit/65d562e958e5f0f285e9f6dadd1ea725 to your computer and use it in GitHub Desktop.
class DashboardFragment : Fragment() {
companion object {
const val CLIENT_ID = "xxxxx"
const val REDIRECT_URI = "asoview://www.example.com/oauth2redirect/"
}
private var _authService: AuthorizationService? = null
private var _authState: AuthState? = null
// 省略
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// 省略
// AppAuth設定
val serviceConfig = AuthorizationServiceConfiguration(
Uri.parse("https://www.example.com/app/v1/authorization"), // authorization endpoint
Uri.parse("https://www.example.com/app/v1/token") // token endpoint
)
_authState = AuthState(serviceConfig)
_authService = AuthorizationService(requireContext())
// 省略
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
DashboardCompose()
}
}
}
private fun login() {
// アプリ内ブラウザでOAuth2の認証を開始する
// なお、ネイティブアプリ埋め込みのWebViewで認証エンドポイントとやりとりする方法は推奨されていない。(rfc8252の8.12.を参照)
val authRequest = AuthorizationRequest.Builder(
_authState!!.authorizationServiceConfiguration!!,
CLIENT_ID,
ResponseTypeValues.CODE,
Uri.parse(REDIRECT_URI)
).setScope("openid profile phone email").build()
val authIntent = _authService!!.getAuthorizationRequestIntent(authRequest)
activityResult.launch(authIntent)
}
@Composable
private fun DashboardCompose(dashBoadViewModel: DashboardViewModel = viewModel()) {
Button(onClick = { login() }, modifier = Modifier.wrapContentSize(Alignment.TopStart)) {
Text("login")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment