Merge pull request #73 from Erant-s-r-o/otafix

fix
This commit is contained in:
Ota-Prokopec 2023-03-14 18:36:58 +01:00 committed by GitHub
commit 2a781be8e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 12 deletions

View File

@ -12,7 +12,7 @@
let isMounted = false let isMounted = false
$: isReady = $localeLoading === false && $authLoading === false && isMounted $: isReady = $localeLoading === false && $authLoading === false && isMounted
$: if (isReady && $user?.emailVerification) { $: if (isReady && !$user?.emailVerification) {
console.log('change') console.log('change')
if (!location.pathname.startsWith('/login') && !location.pathname.startsWith('/register')) { if (!location.pathname.startsWith('/login') && !location.pathname.startsWith('/register')) {

View File

@ -0,0 +1,10 @@
<script lang="ts">
export let disabled: boolean = false
let className = ''
export { className as class }
</script>
<button {disabled} class={`bg-blue-500 rounded-xl text-white p-4 ${!disabled ? '' : 'bg-gray-500 text-white cursor-default'} ${className}`} on:click>
<slot />
</button>

View File

@ -0,0 +1,8 @@
export const errors = {
1001: 'Nickname is already taken',
409: 'A user with the same email already is in Erant',
}
export const getErrorMessage = (code: number) => {
return errors[code]
}

View File

@ -0,0 +1,6 @@
import { databases } from '$lib/appwrite'
import { Query } from 'appwrite'
export const getUserByErantId = async (erantId: string) => {
return (await databases.listDocuments('63ded6c18e8493bffc83', 'Users', [Query.equal('erantId', erantId)])).documents[0]
}

View File

@ -12,7 +12,7 @@
userName: $user.name, userName: $user.name,
userId: $user.$id, userId: $user.$id,
}, },
[Permission.delete(Role.user($user.$id)), Permission.update(Role.user($user.$id))], [Permission.delete(Role.user($user.$id)), Permission.update(Role.user($user.$id)), Permission.read(Role.users('verified'))],
) )
navigate('/') navigate('/')
})() })()

View File

@ -1,8 +1,10 @@
<script lang="ts"> <script lang="ts">
import { account, user } from '$lib/appwrite' import { account, user } from '$lib/appwrite'
import Loading from '$lib/components/Common/Loading.svelte' import Loading from '$lib/components/Common/Loading.svelte'
import Button from '$lib/components/erant/Button.svelte'
import { navigate } from '$lib/router' import { navigate } from '$lib/router'
import Error from '$root/src/__error.svelte' import { getErrorMessage } from '$lib/utils/appwrite/authorizationErrors'
import { getUserByErantId } from '$lib/utils/database/users'
import { ID } from 'appwrite' import { ID } from 'appwrite'
import { Helper, Checkbox } from 'flowbite-svelte' import { Helper, Checkbox } from 'flowbite-svelte'
import HiddenInput from '../../lib/components/Inputs/Hidden_Input.svelte' import HiddenInput from '../../lib/components/Inputs/Hidden_Input.svelte'
@ -17,18 +19,24 @@
let state: 'email-sent' | 'register' | 'loading' = 'register' let state: 'email-sent' | 'register' | 'loading' = 'register'
let error: string | null = null let error: string | null = null
$: buttonCodition = name.length > 0 && email.length > 0 && password.length >= 8 && password === repeatPassword && erantId.length > 2
const register = async () => { const register = async () => {
//if (password === repeatPassword || name.length < 8 || email.length < 8) throw new Error('conditions are not fine') //if (password === repeatPassword || name.length < 8 || email.length < 8) throw new Error('conditions are not fine')
try { try {
state = 'loading' state = 'loading'
error = null error = null
if (await getUserByErantId(erantId)) {
const err = new Error('fdjsaůl')
err['code'] = 1001
throw err
}
await account.create(ID.unique(), email, password, name) await account.create(ID.unique(), email, password, name)
await account.createEmailSession(email, password) await account.createEmailSession(email, password)
await account.createVerification(`${location.origin}/register/emailverification/${erantId}`) await account.createVerification(`${location.origin}/register/emailverification/${erantId}`)
state = 'email-sent' state = 'email-sent'
} catch (err) { } catch (err) {
error = JSON.parse(JSON.stringify(err)).response.message error = getErrorMessage(err.code)
state = 'register' state = 'register'
} }
} }
@ -44,7 +52,7 @@
<div class="form"> <div class="form">
{#if error} {#if error}
<Helper color="red">{error}</Helper> <Helper class="ml-4" color="red">{error}</Helper>
{/if} {/if}
<div class="inform"> <div class="inform">
<input bind:value={name} type="text" placeholder="Name" autocomplete="full-name" required /> <input bind:value={name} type="text" placeholder="Name" autocomplete="full-name" required />
@ -53,18 +61,26 @@
<HiddenInput bind:value={password} placeholder="Password" /> <HiddenInput bind:value={password} placeholder="Password" />
<HiddenInput bind:value={repeatPassword} placeholder="Re-type password" /> <HiddenInput bind:value={repeatPassword} placeholder="Re-type password" />
<div class="flex items-center"> <div class="flex items-center">
<input id="link-checkbox" type="checkbox" value="" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600" required> <input
<label for="link-checkbox" class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">I agree with the <a href="erant.cz/terms-and-conditions" class="text-blue-600 dark:text-blue-500 hover:underline">terms and conditions</a>, id="link-checkbox"
type="checkbox"
value=""
class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600"
required
/>
<label for="link-checkbox" class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300"
>I agree with the <a href="erant.cz/terms-and-conditions" class="text-blue-600 dark:text-blue-500 hover:underline">terms and conditions</a>,
<a href="erant.cz/privacy-policy" class="text-blue-600 dark:text-blue-500 hover:underline">privacy policy</a> and <a href="erant.cz/privacy-policy" class="text-blue-600 dark:text-blue-500 hover:underline">privacy policy</a> and
<a href="erant.cz/cookie-policy" class="text-blue-600 dark:text-blue-500 hover:underline">Cookie Policy</a>, which this site uses.</label> <a href="erant.cz/cookie-policy" class="text-blue-600 dark:text-blue-500 hover:underline">Cookie Policy</a>, which this site uses.</label
>
</div> </div>
<button class="loginButton" on:click={() => register()}> <Button class="w-full text-2xl" disabled={!buttonCodition} on:click={() => register()}>
{#if state === 'loading'} {#if state === 'loading'}
<Loading class="text-white" /> <Loading class="text-white" />
{:else} {:else}
Sign up Sign up
{/if} {/if}
</button> </Button>
</div> </div>
</div> </div>