Skip to content

Commit

Permalink
feat: Add solutions (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirland authored Oct 23, 2023
1 parent 779353d commit 5ed27db
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 63 deletions.
18 changes: 18 additions & 0 deletions solution/1_supabase_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Project Setup

1. Add the dependency `flutter pub add supabase_flutter`
2. Create [Xmartchat Supabase project](https://supabase.com/dashboard)
3. Initialize Supabase in your app

```dart
late SupabaseClient supabaseClient;
Future<void> _initSupabase() async {
await Supabase.initialize(
url: 'YOUR_SUPABASE_URL',
anonKey: 'YOUR_SUPABASE_ANON_KEY',
);
supabaseClient = Supabase.instance.client;
}
```
39 changes: 39 additions & 0 deletions solution/2_authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Authentication

[Docs](https://supabase.com/docs/reference/dart/auth-signup)

1. Setup email provider
2. Implament methods to manage the state, sign up, sign in and sign out.

```dart
class AuthRemoteSourceImpl implements AuthRemoteSource {
@override
Stream<String?> getUserId() => supabaseClient.auth.onAuthStateChange
.map((event) => event.session)
.startWith(supabaseClient.auth.currentSession)
.map((event) => event?.user.id)
.distinct();
@override
Future<void> signIn({
required String email,
required String password,
}) =>
supabaseClient.auth.signInWithPassword(email: email, password: password);
@override
Future<void> signUp({
required String alias,
required String email,
required String password,
}) =>
supabaseClient.auth.signUp(
email: email,
password: password,
data: {'alias': alias},
);
@override
Future<void> signOut() => supabaseClient.auth.signOut();
}
```
74 changes: 11 additions & 63 deletions mobile/SOLUTION_CODE.md → solution/3_messages.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,3 @@
## Project Setup

[DOCS](https://supabase.com/docs/reference/dart/installing)

1. Add the dependency `flutter pub add supabase_flutter`

2. Initialize Supabase in your app

```dart
late SupabaseClient supabaseClient;
Future<void> _initSupabase() async {
await Supabase.initialize(
url: 'YOUR_SUPABASE_URL',
anonKey: 'YOUR_SUPABASE_ANON_KEY',
);
supabaseClient = Supabase.instance.client;
}
```

## Authentication

[Docs](https://supabase.com/docs/reference/dart/auth-signup)

```dart
class AuthRemoteSourceImpl implements AuthRemoteSource {
@override
Stream<String?> getUserId() => supabaseClient.auth.onAuthStateChange
.map((event) => event.session)
.startWith(supabaseClient.auth.currentSession)
.map((event) => event?.user.id)
.distinct();
@override
Future<void> signIn({
required String email,
required String password,
}) =>
supabaseClient.auth.signInWithPassword(email: email, password: password);
@override
Future<void> signUp({
required String alias,
required String email,
required String password,
}) =>
supabaseClient.auth.signUp(
email: email,
password: password,
data: {'alias': alias},
);
@override
Future<void> signOut() => supabaseClient.auth.signOut();
}
```

## Send messages

```dart
Expand Down Expand Up @@ -92,7 +34,14 @@ Future<List<UserMessage>> getMessages() async {

#### Create users table and insert data

Migrate data:
The following script creates:
- Create users table
- Enable real time to listen the table changes
- Insert current user profiles into users table
- Create a trigger to insert user profile into users table



```sql
-- Create User Table
CREATE TABLE
Expand Down Expand Up @@ -159,6 +108,8 @@ Future<List<UserMessage>> getMessages() async {

### Read messages in real time

Listen for messages changes:

```dart
@override
Stream<List<Message>> getMessagesStream() =>
Expand All @@ -169,6 +120,7 @@ Stream<List<Message>> getMessagesStream() =>
.map(Message.fromJsonList);
```

Listen for messages updates:
```dart
@override
Stream<List<UserResponse>> getUsersStream() => _supabaseClient
Expand All @@ -179,10 +131,6 @@ Stream<List<Message>> getMessagesStream() =>
## Uppercase messages

```ts
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { createClient } from "https://esm.sh/@supabase/[email protected]?target=deno";

Expand Down
10 changes: 10 additions & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Solutions for Supabase Workshop
Welcome to the Solutions folder of our Supabase workshop! Here you will find detailed solutions for the different parts of the workshop, providing step-by-step guidance to help you master serverless architecture and real-time communication using Supabase and Xmartchat.

Contents:
- **[1_supabase_setup.md](./1_supabase_setup.md):** This file contains the solution for setting up your Supabase project, including database configuration and environment setup.

- **[2_authentication.md](./2_authentication.md):** Explore this document for the solution to implementing authentication features within Xmartchat, ensuring secure user access and interaction.

- **[3_messages.md](3_messages.md):** Here, you'll find the solution for enabling real-time messaging functionality in Xmartchat, allowing users to send and receive messages instantaneously.

0 comments on commit 5ed27db

Please sign in to comment.