Last active
March 30, 2023 18:00
-
-
Save subzey/eb390a1ce0af538fe79ae762103cb294 to your computer and use it in GitHub Desktop.
Login 200 error
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head><title>Login status demo</title></head> | |
<body> | |
<fieldset> | |
<legend>Псевдо-логинка</legend> | |
<form id="loginform"> | |
<p><label><input name="user" type="input"> Логин</label></p> | |
<p><label><input name="password" type="password"> Пароль</label></p> | |
<p><input type="submit"></p> | |
</form> | |
</fieldset> | |
<script> | |
document.querySelector('#loginform').addEventListener( | |
'submit', | |
async (e) => { | |
e.preventDefault(); | |
const res = await fetch('/api/login', { method: 'POST', body: new FormData(e.target) }); | |
const { error } = await res.json(); | |
if (!res.ok || error) { | |
alert('Неправильный логин или пароль!'); | |
} else { | |
alert('Залогинились!'); | |
} | |
}, | |
false | |
); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
#!/usr/bin/env node | |
import { createServer } from 'http'; | |
import { createReadStream } from 'fs'; | |
import { resolve } from 'path'; | |
import { URL, fileURLToPath } from 'url'; | |
function apiResponse(req, res) { | |
res.writeHead(200, { 'content-type': 'application/json;charset=utf-8' }); | |
res.end(JSON.stringify({ | |
error: 'Wrong credentials', | |
})); | |
} | |
function serveIndex(req, res) { | |
const indexPath = resolve(fileURLToPath(import.meta.url), '../index.html'); | |
console.log(indexPath); | |
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' }); | |
createReadStream(indexPath).pipe(res); | |
} | |
const srv = createServer((req, res) => { | |
const { pathname } = new URL(req.url, 'http://localhost/'); | |
switch (pathname) { | |
case '/': | |
return serveIndex(req, res); | |
case '/api/login': | |
if (req.method !== 'POST') { | |
res.writeHead(405, { allow: 'POST' }); | |
res.end(); | |
return; | |
} | |
return apiResponse(req, res); | |
default: | |
res.writeHead(404); | |
res.end(); | |
} | |
}); | |
srv.on('listening', () => { console.log(`http://127.0.0.1:${srv.address().port}/`); }) | |
srv.listen(); |
This file contains hidden or 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
{ | |
"type": "module", | |
"name": "auth-api-demo", | |
"version": "0.1.0", | |
"bin": "./index.mjs" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment