2021-07-31 23:06:14 +03:00
|
|
|
<script context="module">
|
|
|
|
|
export function preload(page, { config }) {
|
|
|
|
|
return { config };
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2020-12-24 20:32:05 +02:00
|
|
|
<script>
|
2021-07-31 23:06:14 +03:00
|
|
|
import { onMount } from 'svelte';
|
|
|
|
|
import { stores, goto } from '@sapper/app';
|
2021-07-26 02:31:00 +03:00
|
|
|
import Tailwindcss from '../components/Tailwindcss.svelte';
|
2021-07-27 22:53:51 +03:00
|
|
|
import Navigation from '../components/Navigation/index.svelte';
|
2021-05-22 12:32:32 +03:00
|
|
|
// import SideBar from "../components/SideBar/index.svelte";
|
2021-08-18 19:47:02 +03:00
|
|
|
// import Footer from '../components/Footer/index.svelte';
|
2021-07-26 02:31:00 +03:00
|
|
|
import Apps from '../components/Apps/index.svelte';
|
2021-07-31 23:06:14 +03:00
|
|
|
import { handleAuthChange } from '../utils/functions/api';
|
|
|
|
|
import { user, profile } from '../utils/store/user';
|
|
|
|
|
import { getSupabase } from '../utils/functions/supabase';
|
2020-12-24 20:32:05 +02:00
|
|
|
|
2021-04-03 16:43:02 +03:00
|
|
|
export let segment;
|
2021-07-31 23:06:14 +03:00
|
|
|
export let config;
|
2021-07-12 05:55:57 +03:00
|
|
|
|
2021-07-31 23:06:14 +03:00
|
|
|
let supabase = getSupabase(config);
|
2021-07-12 05:55:57 +03:00
|
|
|
let { page } = stores();
|
2021-07-31 23:06:14 +03:00
|
|
|
let path = $page.path.replace('/', '');
|
|
|
|
|
|
|
|
|
|
async function getProfile() {
|
2021-08-18 19:47:02 +03:00
|
|
|
console.log('Get user profile');
|
2021-07-31 23:06:14 +03:00
|
|
|
// Get user profile
|
|
|
|
|
const authUser = supabase.auth.user();
|
|
|
|
|
|
|
|
|
|
// Check if user has profile
|
|
|
|
|
let {
|
|
|
|
|
data: profileData,
|
|
|
|
|
error,
|
|
|
|
|
status,
|
|
|
|
|
} = await supabase
|
|
|
|
|
.from('profile')
|
2021-08-18 19:47:02 +03:00
|
|
|
.select(`*, org:organizationmember(*)`)
|
|
|
|
|
.eq('id', authUser.id)
|
2021-07-31 23:06:14 +03:00
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
if (error && !profileData && status === 406) {
|
|
|
|
|
// User wasn't found, create profile
|
2021-08-18 19:47:02 +03:00
|
|
|
console.log(`User wasn't found, create profile`);
|
2021-07-31 23:06:14 +03:00
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
const [regexUsernameMatch] = [...authUser.email.matchAll(/(.*)@/g)];
|
|
|
|
|
|
|
|
|
|
const { data, error } = await supabase.from('profile').insert({
|
|
|
|
|
id: authUser.id,
|
|
|
|
|
username: regexUsernameMatch[1],
|
|
|
|
|
fullname: regexUsernameMatch[1],
|
|
|
|
|
});
|
2021-07-31 23:06:14 +03:00
|
|
|
|
|
|
|
|
// Profile created, go to profile page
|
|
|
|
|
if (!error && data) {
|
2021-08-18 19:47:02 +03:00
|
|
|
$user.fetchingUser = false;
|
|
|
|
|
$user.isLoggedIn = true;
|
|
|
|
|
$user.currentSession = authUser;
|
|
|
|
|
|
|
|
|
|
profile.set(data[0]);
|
|
|
|
|
|
|
|
|
|
// If user coming to login page, then
|
|
|
|
|
if (path.includes('login') || path.includes('signup')) {
|
|
|
|
|
goto('/dashboard');
|
|
|
|
|
}
|
2021-07-31 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
} else if (profileData) {
|
|
|
|
|
// Profile exists, go to profile page
|
2021-08-18 19:47:02 +03:00
|
|
|
$user.fetchingUser = false;
|
|
|
|
|
$user.isLoggedIn = true;
|
|
|
|
|
$user.currentSession = authUser;
|
|
|
|
|
console.log(`profileData`, profileData);
|
2021-07-31 23:06:14 +03:00
|
|
|
profile.set(profileData);
|
2021-08-18 19:47:02 +03:00
|
|
|
|
|
|
|
|
// If user coming to login page, then
|
|
|
|
|
if (path.includes('login') || path.includes('signup')) {
|
|
|
|
|
goto('/dashboard');
|
|
|
|
|
}
|
2021-07-31 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
2021-08-18 19:47:02 +03:00
|
|
|
console.log('mounting layout');
|
2021-07-31 23:06:14 +03:00
|
|
|
const { data: authListener } = supabase.auth.onAuthStateChange(
|
|
|
|
|
(event, session) => {
|
|
|
|
|
handleAuthChange(event, session);
|
|
|
|
|
|
|
|
|
|
if (event === 'SIGNED_IN') {
|
2021-08-18 19:47:02 +03:00
|
|
|
$user.fetchingUser = true;
|
2021-07-31 23:06:14 +03:00
|
|
|
getProfile();
|
|
|
|
|
} else {
|
2021-08-18 19:47:02 +03:00
|
|
|
console.log('not logged in, go to login');
|
2021-07-31 23:06:14 +03:00
|
|
|
return goto('/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
authListener.unsubscribe();
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$: path = $page.path.replace('/', '');
|
2020-12-24 20:32:05 +02:00
|
|
|
</script>
|
|
|
|
|
|
2021-05-07 03:43:37 +03:00
|
|
|
<Tailwindcss />
|
|
|
|
|
|
|
|
|
|
<!-- <Nav {segment} /> -->
|
|
|
|
|
|
2021-07-27 22:53:51 +03:00
|
|
|
<main>
|
2021-08-18 19:47:02 +03:00
|
|
|
{#if !['login', 'signup'].includes(path)}
|
2021-07-27 22:53:51 +03:00
|
|
|
<Navigation {segment} />
|
2021-07-26 02:31:00 +03:00
|
|
|
{/if}
|
2021-07-31 23:06:14 +03:00
|
|
|
|
2021-07-27 22:59:41 +03:00
|
|
|
<div class="flex justify-between">
|
|
|
|
|
<slot />
|
2021-07-31 23:06:14 +03:00
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
<!-- {#if path.includes('courses')}
|
2021-07-27 22:59:41 +03:00
|
|
|
<Apps />
|
2021-08-18 19:47:02 +03:00
|
|
|
{/if} -->
|
2021-07-27 22:59:41 +03:00
|
|
|
</div>
|
2021-05-07 03:43:37 +03:00
|
|
|
</main>
|
|
|
|
|
|
2021-07-31 23:06:14 +03:00
|
|
|
{#if !['about', ''].includes(path)}
|
2021-08-18 19:47:02 +03:00
|
|
|
<!-- <Footer {segment} /> -->
|
2021-07-31 23:06:14 +03:00
|
|
|
{/if}
|
2021-05-07 03:43:37 +03:00
|
|
|
|
2020-12-24 20:32:05 +02:00
|
|
|
<style>
|
2020-12-30 22:29:01 +02:00
|
|
|
main {
|
|
|
|
|
background-color: white;
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
}
|
2020-12-26 02:22:37 +02:00
|
|
|
|
2021-04-03 16:43:02 +03:00
|
|
|
:global(a:hover) {
|
|
|
|
|
text-decoration: underline;
|
|
|
|
|
}
|
2021-05-06 08:22:25 +03:00
|
|
|
:global(:root) {
|
2021-07-28 22:29:23 +03:00
|
|
|
--main-primary-color: rgba(29, 78, 216, 1);
|
2021-05-06 08:22:25 +03:00
|
|
|
--border-color: #eaecef;
|
|
|
|
|
}
|
2020-12-24 20:32:05 +02:00
|
|
|
</style>
|