Compare commits
6 Commits
9c59401cf2
...
ffbfe2ddd0
Author | SHA1 | Date | |
---|---|---|---|
|
ffbfe2ddd0 | ||
d490208ffa | |||
27e75c4497 | |||
a965adfdce | |||
6a4e161d71 | |||
b98371cf70 |
@ -20,6 +20,11 @@ source .env/bin/activate
|
|||||||
``` bash
|
``` bash
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
```
|
```
|
||||||
|
- Setup database
|
||||||
|
```
|
||||||
|
python manage.py runserver makemigrations profiles
|
||||||
|
python manage.py migrate profiles
|
||||||
|
```
|
||||||
- Start the developpement server
|
- Start the developpement server
|
||||||
```
|
```
|
||||||
python manage.py runserver 0.0.0.0:8000
|
python manage.py runserver 0.0.0.0:8000
|
||||||
|
12
frontend/static/css/accounts/login.css
Normal file
12
frontend/static/css/accounts/login.css
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#app .form {
|
||||||
|
background-color: red;
|
||||||
|
width: 300px;
|
||||||
|
height: 300px;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(1, 1fr);
|
||||||
|
grid-gap: 10px;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-top: 90px;
|
||||||
|
border: 15px black solid;
|
||||||
|
}
|
12
frontend/static/css/accounts/register.css
Normal file
12
frontend/static/css/accounts/register.css
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#app .form {
|
||||||
|
background-color: red;
|
||||||
|
width: 300px;
|
||||||
|
height: 300px;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(1, 1fr);
|
||||||
|
grid-gap: 10px;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-top: 90px;
|
||||||
|
border: 15px black solid;
|
||||||
|
}
|
15
frontend/static/js/api/accounts.js
Normal file
15
frontend/static/js/api/accounts.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
class Accounts
|
||||||
|
{
|
||||||
|
constructor (client)
|
||||||
|
{
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(username, password)
|
||||||
|
{
|
||||||
|
let response = await this.client._post("/api/accounts/register", {username: username, password: password});
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Accounts }
|
@ -1,8 +1,20 @@
|
|||||||
|
import { Accounts } from "./accounts.js";
|
||||||
|
|
||||||
|
function extract_token(response)
|
||||||
|
{
|
||||||
|
let cookies = response.headers.get("set-cookie");
|
||||||
|
if (cookies == null)
|
||||||
|
return null;
|
||||||
|
token = cookies.slice(cookies.indexOf("=") + 1, cookies.indexOf(';'))
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
constructor(url)
|
constructor(url)
|
||||||
{
|
{
|
||||||
this._url = url;
|
this._url = url;
|
||||||
|
this.accounts = new Accounts(this);
|
||||||
this._token = undefined;
|
this._token = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,12 +32,16 @@ class Client
|
|||||||
},
|
},
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
let token = extract_token(response);
|
||||||
|
if (token != null)
|
||||||
|
this.token = token;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async login(username, password)
|
async login(username, password)
|
||||||
{
|
{
|
||||||
return this._post("/api/accounts/login", {username: username, password: password})
|
let response = await this._post("/api/accounts/login", {username: username, password: password})
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import Settings from "./views/Settings.js";
|
|||||||
import Chat from "./views/Chat.js";
|
import Chat from "./views/Chat.js";
|
||||||
|
|
||||||
import { Client } from "./api/client.js";
|
import { Client } from "./api/client.js";
|
||||||
|
import RegisterView from "./views/accounts/RegisterView.js";
|
||||||
|
|
||||||
let client = new Client(location.protocol + "//" + location.host)
|
let client = new Client(location.protocol + "//" + location.host)
|
||||||
|
|
||||||
@ -31,8 +32,9 @@ const router = async () => {
|
|||||||
{ path: "/posts", view: Posts },
|
{ path: "/posts", view: Posts },
|
||||||
{ path: "/posts/:id", view: PostView },
|
{ path: "/posts/:id", view: PostView },
|
||||||
{ path: "/settings", view: Settings },
|
{ path: "/settings", view: Settings },
|
||||||
{ path: "/chat", view: Chat },
|
|
||||||
{ path: "/login", view: LoginView },
|
{ path: "/login", view: LoginView },
|
||||||
|
{ path: "/register", view: RegisterView },
|
||||||
|
{ path: "/chat", view: Chat },
|
||||||
];
|
];
|
||||||
|
|
||||||
// Test each route for potential match
|
// Test each route for potential match
|
||||||
|
@ -7,10 +7,18 @@ async function login()
|
|||||||
let password = document.getElementById("password").value;
|
let password = document.getElementById("password").value;
|
||||||
|
|
||||||
let response = await client.login(username, password);
|
let response = await client.login(username, password);
|
||||||
let errors = await response.json();
|
let response_data = await response.json();
|
||||||
|
|
||||||
errors.user.forEach(error => {
|
["username", "user", "password"].forEach(error_field => {
|
||||||
console.log(error)
|
let error_display = document.getElementById(`error_${error_field}`);
|
||||||
|
if (error_display != null)
|
||||||
|
error_display.innerHTML = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(response_data).forEach(error_field => {
|
||||||
|
let error_display = document.getElementById(`error_${error_field}`);
|
||||||
|
if (error_display != null)
|
||||||
|
error_display.innerHTML = response_data[error_field];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,18 +29,23 @@ export default class extends AbstractView {
|
|||||||
document.body.addEventListener("click", e => {
|
document.body.addEventListener("click", e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (e.target.type == "button")
|
if (e.target.type == "button")
|
||||||
{
|
|
||||||
login();
|
login();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getHtml() {
|
async getHtml() {
|
||||||
return `
|
return `
|
||||||
<input type="text" id="username" placeholder="username">
|
<div class=form>
|
||||||
<input type="password" id="password" placeholder="password">
|
<label>Login</label>
|
||||||
<input type="button" value="login">
|
<link rel="stylesheet" href="static/css/accounts/login.css">
|
||||||
<span id="login_failed"></span>
|
<input type="text" id="username" placeholder="username">
|
||||||
|
<span id="error_username"></span>
|
||||||
|
<input type="password" id="password" placeholder="password">
|
||||||
|
<span id="error_password"></span>
|
||||||
|
<input type="button" value="login">
|
||||||
|
<span id="error_user"></span>
|
||||||
|
<a href="/register" class="nav__link" data-link>Register</a>
|
||||||
|
</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
51
frontend/static/js/views/accounts/RegisterView.js
Normal file
51
frontend/static/js/views/accounts/RegisterView.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import AbstractView from "../AbstractView.js";
|
||||||
|
import { client } from "../../index.js";
|
||||||
|
|
||||||
|
async function register()
|
||||||
|
{
|
||||||
|
let username = document.getElementById("username").value;
|
||||||
|
let password = document.getElementById("password").value;
|
||||||
|
|
||||||
|
let response = await client.accounts.create(username, password);
|
||||||
|
let response_data = await response.json();
|
||||||
|
|
||||||
|
["username", "user", "password"].forEach(error_field => {
|
||||||
|
let error_display = document.getElementById(`error_${error_field}`);
|
||||||
|
if (error_display != null)
|
||||||
|
error_display.innerHTML = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(response_data).forEach(error_field => {
|
||||||
|
let error_display = document.getElementById(`error_${error_field}`);
|
||||||
|
if (error_display != null)
|
||||||
|
error_display.innerHTML = response_data[error_field];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class extends AbstractView {
|
||||||
|
constructor(params) {
|
||||||
|
super(params);
|
||||||
|
this.setTitle("register");
|
||||||
|
document.body.addEventListener("click", e => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.target.type == "button")
|
||||||
|
register();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getHtml() {
|
||||||
|
return `
|
||||||
|
<div class=form>
|
||||||
|
<label>Register</label>
|
||||||
|
<link rel="stylesheet" href="static/css/accounts/register.css">
|
||||||
|
<input type="text" id="username" placeholder="username">
|
||||||
|
<span id="error_username"></span>
|
||||||
|
<input type="password" id="password" placeholder="password">
|
||||||
|
<span id="error_password"></span>
|
||||||
|
<input type="button" value="register">
|
||||||
|
<span id="error_user"></span>
|
||||||
|
<a href="/login" class="nav__link" data-link>Login</a>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user