A new Kotlin application.
A few resources to get you started if this is your first kotlin project:
documentation - Anonymous login documentation
auth.signInAnonymously()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = auth.currentUser
updateUI(user)
} else {
Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show()
updateUI(null)
}
}
documentation - Email and password login documentation
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = auth.currentUser
updateUI(user)
} else {
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
updateUI(null)
}
}
auth.signInWithEmailAndPassword()
documentation - Phone number login documentation
var storedVerificationId: String? = null
var resendToken : PhoneAuthProvider? = null
callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
signInWithPhoneAuthCredential(credential)
}
override fun onVerificationFailed(e: FirebaseException) { }
override fun onCodeSent(verificationId: String,token: PhoneAuthProvider.ForceResendingToken ) {
storedVerificationId = verificationId
resendToken = token
}
}
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
callbacks) // OnVerificationStateChangedCallbacks
val credential = PhoneAuthProvider.getCredential(verificationId!!, code)
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
auth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = task.result?.user
updateUI(user)
} else {
Log.w(TAG, "signInWithCredential:failure", task.exception)
updateUI(null)
}
}
}
documentation - Google login documentation
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)!!
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {}
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
auth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = auth.currentUser
updateUI(user)
} else {
Snackbar.make(view, "Authentication Failed.", Snackbar.LENGTH_SHORT).show()
updateUI(null)
}
}
}
documentation - Facebook login documentation register your app in Facebook - Facebook for developers
callbackManager = CallbackManager.Factory.create()
binding.buttonFacebookLogin.setReadPermissions("email", "public_profile")
binding.buttonFacebookLogin.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
handleFacebookAccessToken(loginResult.accessToken)
}
override fun onCancel() {
}
override fun onError(error: FacebookException) {
}
})
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager.onActivityResult(requestCode, resultCode, data)
}
private fun handleFacebookAccessToken(token: AccessToken) {
val credential = FacebookAuthProvider.getCredential(token.token)
auth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = auth.currentUser
updateUI(user)
} else {
Toast.makeText(baseContext, "Authentication failed.",Toast.LENGTH_SHORT).show()
updateUI(null)
}
}
}
documentation - Twitter login documentation register your app in Twitter - Twitter for developers
val provider : OAuthProvider.Builder = OAuthProvider.newBuilder("twitter.com")
loginWithTwitter(provider)
fun loginWithTwitter(provider: OAuthProvider.Builder){
auth.startActivityForSignInWithProvider(activity,provider.build())
.addOnSuccessListener { authResult ->
updateUI(auth.currentUser!!)
}
.addOnFailureListener {
updateUI(null)
}
}
documentation - Github login documentation register your app in Github - Github for developers
val provider : OAuthProvider.Builder = OAuthProvider.newBuilder("github.com")
loginWithGithub(provider)
fun loginWithGithub(provider: OAuthProvider.Builder){
auth.startActivityForSignInWithProvider(activity,provider.build())
.addOnSuccessListener { authResult ->
updateUI(auth.currentUser!!)
}
.addOnFailureListener {
updateUI(null)
}
}
documentation - Yahoo login documentation register your app in Yahoo - Yahoo for developers
val provider : OAuthProvider.Builder = OAuthProvider.newBuilder("yahoo.com")
loginWithYahoo(provider)
fun loginWithYahoo(provider: OAuthProvider.Builder){
auth.startActivityForSignInWithProvider(activity,provider.build())
.addOnSuccessListener { authResult ->
updateUI(auth.currentUser!!)
}
.addOnFailureListener {
updateUI(null)
}
}
documentation - Microsoft login documentation register your app in Microsoft - Microsoft for developers
val provider : OAuthProvider.Builder = OAuthProvider.newBuilder("microsoft.com")
loginWithMicrosoft(provider)
fun loginWithMicrosoft(provider: OAuthProvider.Builder){
auth.startActivityForSignInWithProvider(activity,provider.build())
.addOnSuccessListener { authResult ->
updateUI(auth.currentUser!!)
}
.addOnFailureListener {
updateUI(null)
}
}
documentation - Apple login documentation register your app in Apple - Apple for developers
val provider : OAuthProvider.Builder = OAuthProvider.newBuilder("apple.com")
loginWithApple(provider)
fun loginWithApple(provider: OAuthProvider.Builder){
auth.startActivityForSignInWithProvider(activity,provider.build())
.addOnSuccessListener { authResult ->
updateUI(auth.currentUser!!)
}
.addOnFailureListener {
updateUI(null)
}
}