Spaces:
Running
Running
File size: 13,315 Bytes
d54ea4b |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { PUBLIC_APP_ASSETS, PUBLIC_ORIGIN } from "$env/static/public";
import { page } from "$app/stores";
import TextModal from "$lib/components/TextModal.svelte";
import { Checkbox } from "svelte-mui";
import Modal from "$lib/components/BigModal.svelte";
import { api_key_writable, email_addr_writable, is_logged_writable, is_magic_writable } from "../../routes/LayoutWritable";
import { goto } from "$app/navigation";
import { base } from "$app/paths";
import { getApiKey } from "../../routes/tools";
let email = ""; // email for login view
let email2 = ""; // email for magic link view
let password = "";
let showPassword = false;
let magicSuccess = false;
let loginFail = false;
let magicFail = false;
let magicView = false;
let subscribeNewsletter = true;
let error;
let hasAccount = true;
const dispatch = createEventDispatcher<{ close: void }>();
function toggleAccountStatus() {
hasAccount = !hasAccount;
magicView = false;
}
function setMagicView() {
hasAccount = !hasAccount;
magicView = true;
}
// async function isLogged() {
// try {
// const response = await fetch("https://cloud.mithrilsecurity.io/api/auth/getUserInfo", {
// method: "GET",
// credentials: "include",
// headers: {
// "Content-Type": "application/json",
// },
// });
// if (response.ok) {
// const res = await response.text()
// const json: JSON = JSON.parse(res)
// email_addr_writable.set(json.email)
// }
// else {
// // Handle errors here
// console.error("User is not logged in");
// }
// } catch (err) {
// // Handle network errors here
// console.error("Network error", err);
// }
// }
async function sendMagicLink(event: { preventDefault: () => void }) {
event.preventDefault();
const data = {
email: email2,
subscribeNewsletter,
};
try {
const response = await fetch("https://cloud.mithrilsecurity.io/api/auth/blindChatRegister", {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
dataLayer.push({'event': 'signup-start'});
if (response.ok) {
magicSuccess = true;
} else {
magicFail = true;
}
} catch (error) {
console.error("Network error", error);
}
}
async function apiCallLogin(arg: { email: string; password: string; }) {
const data = {
email,
password,
};
let result = {
success:true,
error:"",
}
console.log("logging in...")
try {
const response = await fetch('https://cloud.mithrilsecurity.io/api/auth/login', {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (response.ok)
{
console.log(await response.text())
is_logged_writable.set(true)
api_key_writable.set(await getApiKey());
isLogged()
result.success = true;
}
else {
loginFail = true;
console.error("Login failed");
result.error = "Login failed";
}
} catch (error) {
console.error("Network error", error);
result.error = "Login failed";
}
return result;
}
async function registerUser(arg: { email: string; }) {
const data = {
email,
};
console.log("registering user")
try {
const response = await fetch('https://cloud.mithrilsecurity.io/api/auth/blindChatRegister', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
dataLayer.push({'event': 'signup-start'});
if (response.ok)
{
magicSuccess = true;
api_key_writable.set(await getApiKey());
}
else {
magicFail = true;
}
} catch (error) {
console.error("Network error", error);
}
}
function reloadSession() {
}
function submitForm() {
if(email && password) { // Basic validation
apiCallLogin({email, password}) // TODO: sort out error handling here
.then(result => {
console.log(result)
if(result.success) {
reloadSession();
goto(`${base}/`, { invalidateAll: true });
} else {
error = result.error;
}
});
}
}
// For toggling the password visibility:
function handleClickShowPassword() {
showPassword = !showPassword;
}
</script>
<Modal on:close>
<script>
let overlayComp
<Overlay bind:this={overlayComp} />
overlayComp.setTheme();
</script>
<div class="border rounded-2xl border-mithril-border pt-4 px-12 pb-12 bg-login text-white md:min-w-[450px]">
<div class = "pb-4 flex justify-end">
<div>
<!-- Top right link text -->
<button type="button" class="underline" on:click={toggleAccountStatus}>
{#if hasAccount}
I don't have an account
{:else if magicView}
Return
{:else}
I have an account
{/if}
</button>
</div>
</div>
<div class="flex justify-center">
<!-- Title of modal -->
<div class="font-bold text-3xl text-mithril-yellow">
{#if hasAccount}
Sign in
{:else if magicView}
Sign in
{:else}
Sign up
{/if}
</div>
</div>
<!-- VIEW #1 Classic sign in view -->
{#if hasAccount}
<script>
document.getElementById("login").style.display = "block";
</script>
<div id="login" class="py-4" style="max-width: 350px">
<form on:submit={submitForm}>
<div class="pt-4 flex justify-between items-center flex-wrap gap-2.5 border-1 border-gray">
<!-- Classic sign in view -->
<input id="email1"
class="bg-login rounded-2xl text-white border border-mithril-border p-2 w-full"
type="email" bind:value={email} placeholder="Email" />
</div>
<div class="py-4 flex justify-between items-center flex-wrap gap-2.5 border-1 border-gray relative">
<!-- Text input for the "shown" password -->
<input
type="text"
class="bg-login rounded-2xl text-white border border-mithril-border p-2 w-full"
bind:value={password}
placeholder="Password"
class:hidden={!showPassword}
/>
<!-- Password input for the "hidden" password -->
<input
class="bg-login rounded-2xl text-white border border-mithril-border p-2 w-full"
type="password"
bind:value={password}
placeholder="Password"
class:hidden={showPassword}
/>
{#if !showPassword}
<!-- show password icon -->
<img
class="css-vubbuv toggle-visibility-icon absolute right-3 top-1/2 transform -translate-y-1/2 cursor-pointer"
alt="show password button"
src="{PUBLIC_ORIGIN || $page.url.origin}{base}/{PUBLIC_APP_ASSETS}/show-password.png"
title="show password button"
width="20"
height="20"
on:click={handleClickShowPassword}
/>
{:else}
<!-- TODO: hide password icon -->
<img
class="css-vubbuv toggle-visibility-icon absolute right-3 top-1/2 transform -translate-y-1/2 cursor-pointer"
alt="help password button"
src="{PUBLIC_ORIGIN || $page.url.origin}{base}/{PUBLIC_APP_ASSETS}/hide-password.png"
title="hide password button"
width="20"
height="20"
on:click={handleClickShowPassword}
/>
{/if}
</div>
<!-- Sign in button -->
<div class="py-3 flex justify-center items-center flex-wrap gap-2.5 border-1 border-gray">
<button class="p-3 flex justify-right content-center bg-mithril-yellow text-black rounded-lg min-w-36 py-2 px-3 text-center"
type="submit">
Sign in
</button>
</div>
{#if loginFail}
<TextModal title="Login failed" text="Please check your credentials try again" on:close={() => (loginFail = false)}/>
{/if}
<div class="p-3 underline justify-right text-right">
<!-- TODO Add in forgotten password later
<button on:click={resetPassword}>Forgot Password?</button>
-->
</div>
</form>
<div class="flex items-center justify-center relative">
<div class="absolute top-1/2 left-0 right-0 h-px bg-gray-300"></div>
<span class="MuiDivider-wrapper css-c1ovea relative z-10 px-4 bg-login">OR</span>
</div>
<!-- Magic link screen button -->
<div class="pt-4 pb-0 flex justify-center items-center flex-wrap gap-2.5">
<button class="bg-login rounded-2xl text-white border border-mithril-yellow p-2 w-full"
on:click={setMagicView}>Sign in with magic link ✨</button>
</div>
</div>
<!-- VIEW #2 Magic link -->
{:else if magicView}
<script>
document.getElementById("login").style.display = "none";
</script>
<div class="relative flex flex-col space-y-4 pt-4" style="max-width: 350px">
<div class="py-3 flex justify-between items-center flex-wrap gap-2.5 border-1 border-gray">
<input
class="bg-login rounded-2xl text-white border border-mithril-border p-2 w-full"
type="email" bind:value={email2} placeholder="Email" />
</div>
<div class="flex justify-center items-center flex-wrap gap-2.5 border-1 order-gray">
<button class="p-3 flex content-center bg-yellow-500 text-black rounded-lg min-w-36 py-2 px-3 text-center"
on:click={sendMagicLink}>Sign in with magic link ✉️ </button>
{#if magicSuccess}
<TextModal title="Magic link sent" text="✅ Check your emails for your magic login link" on:close={() => (magicSuccess = false)}/>
{:else if magicFail}
<TextModal title="Error" text="Please check your email address is valid and try again" on:close={() => (magicFail = false)}/>
{/if}
<p class="pt-5">
By using our services, you agree to the{' '}<a class="text-mithril-yellow" href="https://www.mithrilsecurity.io/privacy-policy" target="_blank">Terms of Service</a>.
</p>
</div>
</div>
<!-- VIEW #3 Sign up -->
{:else if !hasAccount && !magicView}
<!-- force get rid of login view in background -->
<script>
document.getElementById("login").style.display = "none";
</script>
<div class="relative flex flex-col space-y-4 pt-4" style="max-width: 350px">
<!-- email input -->
<div class="pt-4 flex justify-between items-center flex-wrap gap-2.5 border-1 border-gray">
<input id="email1"
class="bg-login rounded-2xl text-white border border-mithril-border p-2 w-full"
type="email" bind:value={email} placeholder="Email" />
</div>
<div class="flex justify-center items-center flex-wrap gap-2.5 border-1 order-gray">
<button class="p-3 flex content-center bg-yellow-500 text-black rounded-lg min-w-36 py-2 px-3 text-center"
on:click={registerUser}>Sign up</button>
{#if magicSuccess}
<TextModal title="Thank you" text="✅ Please click on the magic link we have sent you to access your account" on:close={() => (magicSuccess = false)}/>
{:else if magicFail}
<TextModal title="Error" text="Please check your email address is valid and try again" on:close={() => (magicFail = false)}/>
{/if}
<p class="pt-5">
By signing up, you agree to the{' '}<a class="text-mithril-yellow" href="https://www.mithrilsecurity.io/privacy-policy" target="_blank">Terms of Service</a>.
</p>
</div>
</div>
{/if}
</div>
</Modal>
|