Firebase SDK Authentication

Firebase SDK authentication hands-on path है: आप अपनी खुद की login screens build करते हैं और directly FirebaseAuth methods call करते हैं, जैसे createUserWithEmailAndPassword और signInWithEmailAndPassword, आपको UI लिखने के exchange में look और flow पर full control देते हुए।

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

इसे खुद करना

FirebaseUI आपको free में login देता है पर अपनी screens के साथ। कभी-कभी आप अपनी own login screen चाहते हैं, जो आपकी app के design से exactly match करे, और flow पर अपना control। इसके लिए, आप directly Firebase Authentication SDK call करते हैं।

आप खुद login और registration layouts build करते हैं, फिर जब user आपके buttons tap करता है FirebaseAuth methods invoke करते हैं। यह lesson core methods और Firebase इस्तेमाल किया callback pattern दिखाता है। यह FirebaseUI से ज़्यादा code है, पर यही तरीका है complete control पाने का, और यह clear करता है FirebaseUI आपके लिए क्या कर रहा था।

Theory

Core Auth Methods

अपने खुद के UI के साथ, तीन FirebaseAuth methods काम करते हैं:

  • createUserWithEmailAndPassword(email, password): एक नया user register करना।
  • signInWithEmailAndPassword(email, password): एक existing user को log in कराना।
  • signOut(): current user को sign out करना।

चूँकि ये network पर Firebase से बात करते हैं, ये asynchronous हैं: हर एक एक Task return करता है जो बाद में complete होती है, success या failure। आप इस पर react करने के लिए listeners attach करते हैं जब यह finish हो, immediate result पाने की बजाय। तो pattern हमेशा है: method call कीजिए, फिर Task के outcome को handle कीजिए।

Practical

SDK से Register और Login करना

val auth = Firebase.auth

// Register a new user (from your own registration screen)
auth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val user = auth.currentUser        // now signed in
            // navigate to the main screen
        } else {
            // show task.exception?.message on your UI
        }
    }

// Log an existing user in
auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener { task -> /* same success/failure handling */ }

Formula

Async: Task Handle कीजिए, Instant Result Expect मत कीजिए

ये methods user को immediately return नहीं करते। ये एक network request start करते हैं और एक Task return करते हैं; result बाद में एक listener में आता है। तो आप कभी val user = auth.signIn(...) नहीं लिखते, आप method call करते हैं और अपना 'आगे क्या करना है' addOnCompleteListener (या success/failure listeners) के अंदर रखते हैं।

अंदर, task.isSuccessful check कीजिए: अगर true है, user signed in है (auth.currentUser set है) और आप आगे बढ़ते हैं; अगर false है, error दिखाने के लिए task.exception पढ़िए। यह asynchronous, callback-driven style ही है जिससे सभी networked Firebase calls काम करते हैं, तो यहाँ इसमें comfortable होना पूरे subject में काम आता है।

Quiz

Firebase SDK directly इस्तेमाल करते हुए, कौन सा method एक email और password से एक brand-new user register करता है?

  1. signInWithEmailAndPassword(email, password), क्योंकि यह उन्हें sign in कराता है
  2. createUserWithEmailAndPassword(email, password), जो एक नया user account register करता है
  3. signOut(), क्योंकि यह account reset करता है
  4. currentUser, क्योंकि यह user create करता है
Show the answer

createUserWithEmailAndPassword(email, password), जो एक नया user account register करता है

createUserWithEmailAndPassword(email, password) एक नया user account register (create) करता है, और success पर उन्हें sign in करता है। Option A, signInWithEmailAndPassword, एक EXISTING user को log in कराता है; यह नया account create नहीं करता (यह fail होता अगर account exist नहीं करता)। Option C, signOut, current session end करता है; यह कुछ create नहीं करता। Option D, currentUser, एक PROPERTY है जो signed-in user (या null) return करती है; यह state पढ़ती है, user create नहीं करती। Method को action से match कीजिए: register के लिए create, log in के लिए signIn, leave के लिए signOut, कौन signed in है check करने के लिए currentUser।

Think first

Directly Result Return करने की बजाय ये Auth Calls Tasks के साथ Asynchronous क्यों हैं?

signInWithEmailAndPassword एक Task क्यों return करता है जिसे आप listen करते हैं, सिर्फ़ user return करने की बजाय? फिर tap कीजिए।

Show the answer

क्योंकि sign in करने के लिए Firebase के servers से NETWORK पर बात करनी पड़ती है, जो समय लेती है, और wait करते हुए app को block करना user interface freeze कर देगा, तो call asynchronous है और अपना result एक callback के through deliver करता है। जब आप signInWithEmailAndPassword call करते हैं, SDK credentials Firebase को भेजती है, जो इन्हें check करती है और respond करती है, एक round trip जो milliseconds ले सकती है या (एक poor connection पर) seconds। अगर method directly user RETURN करने की कोशिश करती, app का main thread उस network round trip के finish होने का STOP करके wait करना पड़ता, और Android पर main thread UI thread होता है, तो पूरी app freeze हो जाती, कोई scrolling नहीं, कोई taps नहीं, और Android इसे unresponsive होने के लिए kill भी कर सकता है। इसे avoid करने के लिए, Firebase तुरंत एक Task return करता है ('एक result जो बाद में आएगा' represent करते हुए) और app को चलते रहने देता है; जब network response वापस आता है, आपका addOnCompleteListener outcome के साथ fire होता है। यह request in flight रहते हुए UI को smooth रखता है, और आपको एक loading spinner दिखाने देता है, फिर जब यह actually होता है success या failure पर react करने देता है। यह asynchronous, callback-driven model mobile पर network या slow work involve करने वाली किसी भी चीज़ के लिए standard है, exactly इसलिए क्योंकि responsiveness इतनी matter करती है। तो Task extra ceremony नहीं है; यह वह है जो server का wait करते हुए app को alive और smooth रखता है। Network पर कभी UI thread को block मत कीजिए; result तैयार होने पर वापस दीजिए।

Summary

Key takeaways

  • Firebase SDK authentication का मतलब है अपना खुद का login UI build करना और directly FirebaseAuth methods call करना।
  • createUserWithEmailAndPassword एक नया user register करता है; signInWithEmailAndPassword एक existing user को log in कराता है; signOut session end करता है।
  • ये asynchronous हैं: हर एक एक Task return करता है जो बाद में complete होती है, एक immediate result नहीं।
  • एक listener attach कीजिए (addOnCompleteListener) और task.isSuccessful check कीजिए; failure पर error के लिए task.exception पढ़िए।
  • FirebaseUI से बेहतर benefit: UI और flow का full control; cost: आप screens लिखते हैं और states खुद handle करते हैं।
  • Calls async हैं क्योंकि ये network hit करती हैं, और UI thread को block करना app freeze कर देगा।
  • Memory hook: register के लिए create, log in के लिए signIn, एक listener में Task की success या failure handle कीजिए।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Firebase

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Firebase SDK Authentication · Advance Mobile Application Development - II (Major-15-02) · Gri-Learn