📖 Scenario: You are building a simple web app that needs to know who the user is. To do this, you will use Supabase Auth, which helps manage user identity securely and easily.
🎯 Goal: Learn how to set up Supabase Auth to handle user identity by creating a user session, configuring authentication settings, and retrieving the current user's information.
📋 What You'll Learn
Create a Supabase client with the project URL and anon key
Set up authentication configuration with redirect URLs
Write code to sign in a user with email and password
Retrieve and display the current authenticated user's ID
💡 Why This Matters
🌍 Real World
Supabase Auth is used in real apps to securely identify users without building complex authentication systems from scratch.
💼 Career
Understanding how to handle user identity with Supabase Auth is valuable for cloud developers building modern web and mobile applications.
Progress0 / 4 steps
1
Create the Supabase client
Create a variable called supabase by calling createClient with the exact project URL "https://xyzcompany.supabase.co" and anon key "public-anon-key".
Supabase
Hint
Use createClient from @supabase/supabase-js with the exact URL and key.
2
Configure authentication settings
Create a constant called authConfig as an object with a property redirectTo set to "https://yourapp.com/welcome".
Supabase
Hint
Set redirectTo to the welcome page URL where users go after sign-in.
3
Sign in a user with email and password
Write an async function called signInUser that calls supabase.auth.signInWithPassword with an object containing email: 'user@example.com' and password: 'password123', passing authConfig as the second argument.
Supabase
Hint
Use signInWithPassword with email and password, and pass authConfig for redirect.
4
Retrieve and display the current user's ID
Write a function called getCurrentUserId that returns supabase.auth.getUser() and extracts the id from the data.user object.
Supabase
Hint
Use supabase.auth.getUser() to get the current user and return their id.
Practice
(1/5)
1. What is the main purpose of Supabase Auth in an application?
easy
A. To manage user identity and authentication securely
B. To store large files and media
C. To create database tables automatically
D. To handle server backups
Solution
Step 1: Understand Supabase Auth's role
Supabase Auth is designed to handle user login and identity management securely.
Step 2: Compare with other options
Storing files, creating tables, and backups are handled by other Supabase services, not Auth.
Final Answer:
To manage user identity and authentication securely -> Option A
Quick Check:
Supabase Auth = user identity management [OK]
Hint: Auth means user login and identity management [OK]
Common Mistakes:
Confusing Auth with file storage
Thinking Auth creates database tables
Assuming Auth handles backups
2. Which of the following is the correct way to initialize Supabase Auth in a JavaScript app?
easy
A. const supabase = new SupabaseAuth('url', 'key');
B. const supabase = createClient('url', 'key');
C. const supabase = supabase.auth('url', 'key');
D. const supabase = supabaseClient('url', 'key');
Solution
Step 1: Recall Supabase client initialization
Supabase uses createClient(url, key) to initialize the client including Auth.
Step 2: Check other options
Options B, C, and D use incorrect constructors or methods not in Supabase docs.
Final Answer:
const supabase = createClient('url', 'key'); -> Option B
Quick Check:
Use createClient() to initialize Supabase [OK]
Hint: Use createClient() to start Supabase Auth [OK]
Common Mistakes:
Using new keyword incorrectly
Calling auth() as a function to initialize
Misspelling createClient
3. Given this code snippet, what will console.log(user.email) output after a successful login?