Last active
December 13, 2020 05:30
-
-
Save danangponorogo/ce5a9718e5738bccd2340ea2f5884573 to your computer and use it in GitHub Desktop.
Get msg n token from returned json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { url } from "@roxi/routify"; | |
let username = "user"; | |
let password = 123456; | |
let parsed = false; | |
const handleLogin = async () => { | |
let data = `username=${username}&password=${password}`; | |
fetch("http://localhost/@sidak/auth/login", { | |
method: "POST", | |
body: data, | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded", | |
}, | |
}) | |
.then(async (response) => { | |
if (!response.ok) { | |
throw new Error("Network response was not ok."); | |
} else { | |
parsed = await response.json(); | |
} | |
console.log(parsed.msg); | |
// Console log response:, Access granted! | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}; | |
// PHP backend | |
/* | |
$result = array( | |
"msg" => 'Access granted!', | |
"token" => $this->buat_token($data) | |
); | |
echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); | |
*/ | |
</script> | |
<div style="width: 256px; margin: 128px auto; text-align: center"> | |
<h1>Login</h1> | |
<input type="text" bind:value={username} /> | |
<br /> | |
<input type="text" bind:value={password} /> | |
<br /> | |
<button on:click={handleLogin}>Submit</button> | |
<br /> | |
<br /> | |
<br /> | |
<p>This login page is actually located at <a href={$url()}>{$url()}</a></p> | |
<p> | |
You are seeing it here, because we're using $goto with the static option | |
enabled. This renders the login page, without changing the URL in the | |
browser. This | |
<a href={$url('/example')} alt="">link</a> | |
will take you to /example | |
</p> | |
<p>On submit, we're "redirected" to the current URL in your browser.</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment