Skip to content

Commit

Permalink
Merge pull request #52 from JoelBerglund05/authentication
Browse files Browse the repository at this point in the history
Authentication
  • Loading branch information
JoelBerglund05 authored Nov 12, 2024
2 parents 0986eba + c08b252 commit e5390d6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"liveServer.settings.port": 5501
}
"liveServer.settings.port": 5501
}
1 change: 0 additions & 1 deletion js/Authentication.js

This file was deleted.

63 changes: 47 additions & 16 deletions js/DataBase.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

class DataBase {
export default class DataBase {
constructor() {
this.supabase = createClient(
"https://unswumzybkmeifdigbfn.supabase.co",
"KEY",
);

}
async GetARowFrow(table, idToRow) {
try {
const { data, error } = await this.supabase
.from(table)
.select()
.eq('id', idToRow);
console.log(table, data);
return data;

async GetARowFrom(table, idToRow) {
const { data, error } = await this.supabase
.from(table)
.select()
.eq("id", idToRow);

if (error) {
console.log("Kunde inte hämta data: ", error);
return;
}

return data;
}

async SignUpUser(email, password) {
if (password[0] !== password[1]) {
alert("Lösenorden är inte samma");
return;
}

const { data, error } = await this.supabase.auth.signInWithOtp({
email: email,
password: password,
});

if (error) {
console.log("kunde inte skapa konto: ", error);
}
catch (e) {
console.log("An Error has occured: " + event);
}

async SignInUser(email) {
const { data, error } = await this.supabase.auth.signInWithOtp({
email: email,
password: password,
});
if (error) {
console.log("Kunde inte logga in användaren: ", error);
}

}
}

export default DataBase;
async LogOutUser() {
const { error } = await this.supabase.auth.signOut();

if (error) {
console.log("Kunde inte logga ut användaren: ", error);
}
}
}
7 changes: 7 additions & 0 deletions js/EventManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class EventManager {
EventListener(element, eventType, method) {
if (element) {
element.addEventListener(eventType, method);
}
}
}
55 changes: 47 additions & 8 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import DataBase from "./DataBase.js";
import EventManager from "./EventManager.js";

class Main {
constructor() {
this.dataBase = new DataBase();
this.eventManager = new EventManager();
this.btnDBRequest = document.getElementById("btnDBRequest");
this.displayData = document.getElementById("dBData");
this.btnCreateAccount = document.getElementById("createAccount");
this.email = document.getElementById("email");
this.btnSignIn = document.getElementById("signIn");
}
RegisterServiceWorker() {
if ("serviceWorker" in navigator) {
Expand All @@ -13,23 +18,57 @@ class Main {
});
}
}
Main() {
this.RegisterServiceWorker();
async DisplayQuestion(id) {
const DBData = await this.dataBase.GetARowFrow("quizz", id);

this.btnDBRequest.addEventListener("click", async () => {
const DBData = await this.dataBase.GetARowFrow('quizz', 1);

DBData.map(data => {
if (DBData !== null) {
DBData.map((data) => {
this.displayData.innerHTML = data.questions;
});
});
}
}
async CreateAccount() {
this.dataBase.SignUpUser(this.email);

const {
data: { user },
} = await this.dataBase.supabase.auth.getUser();

console.log(user);
}
}

async SignInUser() {
this.dataBase.SignInUser(this.email);

const {
data: { user },
} = await this.dataBase.supabase.auth.getUser();

console.log("HEJJJJJ");

console.log(user);
}
Main() {
const clickEvent = "click";
this.RegisterServiceWorker();

this.eventManager.EventListener(this.btnDBRequest, clickEvent, () =>
this.DisplayQuestion(1),
);

this.eventManager.EventListener(
this.btnCreateAccount,
clickEvent,
this.CreateAccount.bind(this),
);

this.eventManager.EventListener(
this.btnSignIn,
clickEvent,
this.SignInUser.bind(this),
);
}
}

const main = new Main();
main.Main();
16 changes: 16 additions & 0 deletions sign-up.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input id="email" type="text" placeholder="Email" />
<input id="password1" type="text" placeholder="Lösenord" />
<input id="password2" type="text" placeholder="Lösenord" />
<button id="createAccount">Skapa konto</button>
<button id="signIn">Logga in</button>
<script type="module" src="js/main.js"></script>
</body>
</html>

0 comments on commit e5390d6

Please sign in to comment.