-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.sql
More file actions
78 lines (65 loc) · 2.76 KB
/
setup_database.sql
File metadata and controls
78 lines (65 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Activer RLS (Row Level Security)
ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
-- Table pour les profils utilisateur
CREATE TABLE public.user_profiles (
id uuid REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
email text,
username text,
display_name text,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Table pour les mots de passe chiffrés
CREATE TABLE public.passwords (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
user_id uuid REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
title text NOT NULL,
website text,
username text,
password_encrypted text NOT NULL,
notes_encrypted text,
category text DEFAULT 'general',
is_favorite boolean DEFAULT false,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Index pour les performances
CREATE INDEX idx_passwords_user_id ON public.passwords(user_id);
CREATE INDEX idx_passwords_category ON public.passwords(category);
-- Activer RLS sur les tables
ALTER TABLE public.user_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.passwords ENABLE ROW LEVEL SECURITY;
-- Politiques RLS pour user_profiles
CREATE POLICY "Users can view own profile" ON public.user_profiles
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "Users can update own profile" ON public.user_profiles
FOR UPDATE USING (auth.uid() = id);
CREATE POLICY "Users can insert own profile" ON public.user_profiles
FOR INSERT WITH CHECK (auth.uid() = id);
-- Politiques RLS pour passwords
CREATE POLICY "Users can view own passwords" ON public.passwords
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own passwords" ON public.passwords
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own passwords" ON public.passwords
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own passwords" ON public.passwords
FOR DELETE USING (auth.uid() = user_id);
-- Fonction pour créer automatiquement un profil utilisateur
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger AS $$
BEGIN
INSERT INTO public.user_profiles (id, email, username, display_name)
VALUES (
new.id,
new.email,
COALESCE(new.raw_user_meta_data->>'username', split_part(new.email, '@', 1)),
COALESCE(new.raw_user_meta_data->>'display_name', split_part(new.email, '@', 1))
);
RETURN new;
END;
$$ language plpgsql security definer;
-- Trigger pour créer automatiquement le profil
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_user();