Skip to content

Commit

Permalink
improve sample app including web auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Jan 19, 2021
1 parent 3347bc2 commit d3721cf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
48 changes: 42 additions & 6 deletions sample/src/main/java/com/auth0/sample/DatabaseLoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.auth0.android.Auth0
import com.auth0.android.authentication.AuthenticationAPIClient
import com.auth0.android.authentication.AuthenticationException
import com.auth0.android.callback.AuthenticationCallback
import com.auth0.android.callback.Callback
import com.auth0.android.provider.WebAuthProvider
import com.auth0.android.request.DefaultClient
import com.auth0.android.result.Credentials
import com.auth0.sample.databinding.FragmentDatabaseLoginBinding
import com.google.android.material.snackbar.Snackbar
Expand All @@ -18,9 +21,14 @@ import com.google.android.material.snackbar.Snackbar
*/
class DatabaseLoginFragment : Fragment() {

private val apiClient: AuthenticationAPIClient by lazy {
private val account: Auth0 by lazy {
val account = Auth0("esCyeleWIb1iKJUcz6fVR4e29mEHkn0O", "lbalmaceda.auth0.com")
account.isLoggingEnabled = true
// Only enable network traffic logging on production environments!
account.networkingClient = DefaultClient(enableLogging = true)
account
}

private val apiClient: AuthenticationAPIClient by lazy {
AuthenticationAPIClient(account)
}

Expand All @@ -32,22 +40,50 @@ class DatabaseLoginFragment : Fragment() {
binding.buttonLogin.setOnClickListener {
val email = binding.textEmail.text.toString()
val password = binding.textPassword.text.toString()
makeRequest(email, password)
dbLogin(email, password)
}
binding.buttonWebAuth.setOnClickListener {
webAuth()
}
return binding.root
}

private fun makeRequest(email: String, password: String) {
private fun dbLogin(email: String, password: String) {
apiClient.login(email, password, "Username-Password-Authentication")
//Additional customization to the request goes here
.start(object : AuthenticationCallback<Credentials> {
override fun onFailure(error: AuthenticationException) {
Snackbar.make(requireView(), "Failure :(", Snackbar.LENGTH_LONG).show()
Snackbar.make(requireView(), error.getDescription(), Snackbar.LENGTH_LONG)
.show()
}

override fun onSuccess(payload: Credentials?) {
Snackbar.make(
requireView(),
"Success: ${payload!!.accessToken}",
Snackbar.LENGTH_LONG
).show()
}
})
}

private fun webAuth() {
WebAuthProvider.login(account)
.start(requireContext(), object : Callback<Credentials, AuthenticationException> {
override fun onSuccess(payload: Credentials?) {
Snackbar.make(requireView(), "Success :D", Snackbar.LENGTH_LONG).show()
Snackbar.make(
requireView(),
"Success: ${payload!!.accessToken}",
Snackbar.LENGTH_LONG
).show()
}

override fun onFailure(error: AuthenticationException) {
val message =
if (error.isCanceled) "Browser was closed" else error.getDescription()
Snackbar.make(requireView(), message, Snackbar.LENGTH_LONG).show()
}

})
}
}
39 changes: 37 additions & 2 deletions sample/src/main/res/layout/fragment_database_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,54 @@
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Log in using your email and password."
android:text="Log in using your email and password"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textEmail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Log in using Auth0's Universal Login "
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Log in"
android:text="Log in with Username &amp; Password"
app:layout_constraintEnd_toEndOf="@+id/textPassword"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/textPassword"
app:layout_constraintTop_toBottomOf="@+id/textPassword" />

<Button
android:id="@+id/buttonWebAuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Log in with Browser"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="or"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonLogin" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit d3721cf

Please sign in to comment.