Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/primo-af/primo
Browse files Browse the repository at this point in the history
  • Loading branch information
mateomorris committed Aug 27, 2023
2 parents 6041c21 + 29e9dc8 commit d3275a4
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"engines": {
"node": "18.x"
}
}
}
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />

<!-- <link rel="icon" type="image/png" href="/favicon.png" /> -->
<link rel="icon" type="image/png" href="/favicon.png" />
<link rel="stylesheet" href="/codemirror.css" />
<link rel="stylesheet" href="/build/bundle.css" />

Expand Down
62 changes: 39 additions & 23 deletions src/routes/+layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PUBLIC_SUPABASE_PUBLIC_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public'
import {
PUBLIC_SUPABASE_PUBLIC_KEY,
PUBLIC_SUPABASE_URL,
} from '$env/static/public'
import { createSupabaseLoadClient } from '@supabase/auth-helpers-sveltekit'
import { redirect } from '@sveltejs/kit'

Expand All @@ -13,49 +16,62 @@ export async function load(event) {
event: { fetch },
serverSession: event?.data?.session,
})
const { data: { session } } = await supabase.auth.getSession()
const {
data: { session },
} = await supabase.auth.getSession()

if (!session && !event.url.pathname.startsWith('/auth')) {
throw redirect(303, '/auth')
} else if (session) {
// const site = event.params['site']
// const site = event.params['site']
const { sites, user } = await Promise.all([
supabase.from('sites').select('id, name, url, active_deployment, collaborators (*)').order('created_at', { ascending: true }),
supabase.from('users').select('*, server_members (admin, role), collaborators (role)').eq('id', session.user.id).single()
supabase
.from('sites')
.select('id, name, url, active_deployment, collaborators (*)')
.order('created_at', { ascending: true }),
supabase
.from('users')
.select('*, server_members (admin, role), collaborators (role)')
.eq('id', session.user.id)
.single(),
]).then(([{ data: sites }, { data: user }]) => {

const [server_member] = user.server_members
const [collaborator] = user.collaborators

const user_final = server_member ? {
...user,
server_member: true,
admin: server_member.admin,
role: server_member.role,
} : {
...user,
server_member: false,
admin: false,
role: collaborator.role,
}
const user_final = server_member
? {
...user,
server_member: true,
admin: server_member.admin,
role: server_member.role,
}
: {
...user,
server_member: false,
admin: false,
role: collaborator.role,
}

return {
sites: sites || [],
user: user_final
user: user_final,
}
})

// TODO: do this w/ sql
const user_sites = sites?.filter(site =>
/*user is server member*/ user.server_member ||
/*user is site collaborator*/ site.collaborators.some(collaborator => collaborator.user === user.id)
const user_sites = sites?.filter(
(site) =>
/*user is server member*/ user.server_member ||
/*user is site collaborator*/ site.collaborators.some(
(collaborator) => collaborator.user === user.id
)
)

return {
supabase,
session,
user,
sites: user_sites
sites: user_sites,
}
}
}
}
10 changes: 5 additions & 5 deletions src/routes/[site]/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@
storage_subscribe(async ({ bucket, action, key, file, options }) => {
if (action === 'upload') {
const { data } = await supabase.storage
.from(bucket)
.upload(key, file, options)
const { data: res } = supabase.storage.from('images').getPublicUrl(key)
return res.publicUrl
await supabase.storage.from(bucket).upload(key, file, options)
if (bucket === 'images') {
const { data: res } = supabase.storage.from(bucket).getPublicUrl(key)
return res.publicUrl
}
}
})
</script>
Expand Down
41 changes: 25 additions & 16 deletions src/routes/api/invitations/+server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { json } from '@sveltejs/kit';
import { json } from '@sveltejs/kit'
import supabase_admin from '$lib/supabase/admin'

export async function POST({ request }) {
const {url, site = null, server_invitation, role, email} = await request.json();
const {
url,
site = null,
server_invitation,
role,
email,
} = await request.json()

const { data, error } = await supabase_admin.auth.admin.inviteUserByEmail(email, { redirectTo: `${url}/auth/set-password?email=${email}` })
const { data, error } = await supabase_admin.auth.admin.inviteUserByEmail(
email,
{ redirectTo: `${url}/auth/set-password?email=${email}` }
)

if (!error) {
await supabase_admin.from('users').insert({
id: data.user.id,
email: data.user.email,
})

await supabase_admin
.from('users')
.insert({
id: data.user.id,
email: data.user.email
})

// Add to 'server_members' or 'collaborators'
const {error} = server_invitation ?
await supabase_admin.from('server_members').insert({ user: data.user.id, role }) :
await supabase_admin.from('collaborators').insert({ site, user: data.user.id, role })
const { error } = server_invitation
? await supabase_admin
.from('server_members')
.insert({ user: data.user.id, role })
: await supabase_admin
.from('collaborators')
.insert({ site, user: data.user.id, role })

console.error(error)
return json({success: !error, error: error?.message});
return json({ success: !error, error: error?.message })
} else {
console.error(error)
return json({success: false, error: error.message});
return json({ success: false, error: error.message })
}

}
6 changes: 0 additions & 6 deletions src/routes/auth/SignIn.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
<span>Password</span>
<input bind:value={password} type="password" name="password" />
</label>
<input
name="invitation_id"
type="text"
class="hidden"
value={$page.url.searchParams.get('join')}
/>
</div>
<button class="button" type="submit">
{#if !$navigating}
Expand Down
6 changes: 0 additions & 6 deletions src/routes/auth/SignUp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
<span>Confirm Password</span>
<input bind:value={password_confirmation} type="password" />
</label>
<input
name="invitation_id"
type="text"
class="hidden"
value={$page.url.searchParams.get('join')}
/>
</div>
<button class="button" type="submit" {disabled}>
{#if !$navigating}
Expand Down
30 changes: 17 additions & 13 deletions src/routes/auth/set-password/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import supabase_admin from '$lib/supabase/admin'
import {redirect} from '@sveltejs/kit'
import { redirect } from '@sveltejs/kit'

/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ request, locals }) => {
const { session } = locals
default: async ({ request, locals: { supabase } }) => {
const form_data = await request.formData()
const email = form_data.get('email')
const password = form_data.get('password')

const form_data = await request.formData();
const email = form_data.get('email');
const password = form_data.get('password');
const { data } = await supabase_admin
.from('users')
.select('id')
.eq('email', email)
.single()

const { data, error } = await supabase_admin.auth.admin.updateUserById(
session.user.id,
{ password }
)
const { error } = await supabase_admin.auth.admin.updateUserById(data.id, {
password,
})

await supabase_admin.from('invitations').delete().match({ email })

if (!error) {
await supabase.auth.signInWithPassword({ email, password })
throw redirect(303, '/')
}

return { success: !error, error };
}
};
return { success: !error, error }
},
}
15 changes: 3 additions & 12 deletions src/routes/auth/set-password/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@
<div class="fields">
<label>
<span>Email</span>
<input bind:value={email} type="text" name="email" disabled />
<input bind:value={email} type="email" disabled />
<input value={email} type="hidden" name="email" />
</label>
<label>
<span>Password</span>
<input bind:value={password} type="password" name="email" />
<input bind:value={password} type="password" />
</label>
<label>
<span>Confirm Password</span>
<input bind:value={password_again} type="password" name="password" />
</label>
<input
name="invitation_id"
type="text"
class="hidden"
value={$page.url.searchParams.get('join')}
/>
</div>
<button
class="button"
Expand Down Expand Up @@ -137,10 +132,6 @@
padding: 0.75rem;
background-color: #1c1c1c;
font-size: 1rem;
&.hidden {
display: none;
}
}
.button {
Expand Down

0 comments on commit d3275a4

Please sign in to comment.