-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
127 additions
and
82 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,3 @@ | ||
import cookie from "react-cookies"; | ||
|
||
export const SERVER_URL = "http://127.0.0.1:8102"; | ||
|
||
export class User { | ||
static async checkEmail(email: string): Promise<boolean> { | ||
//request | ||
var requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user/email/" + email, requestOptions); | ||
|
||
return await response.text() == "exists"; | ||
} | ||
|
||
static async creatUser(email: string, password: string, username: string, code: string): Promise<[boolean, string]> { | ||
|
||
let formData = new FormData(); | ||
formData.append("email", email); | ||
formData.append("password", password); | ||
formData.append("username", username); | ||
formData.append("code", code); | ||
|
||
let requestOptions: RequestInit = { | ||
method: 'POST', | ||
body: formData, | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user", requestOptions); | ||
let text = await response.text(); | ||
|
||
return [response.ok, text]; | ||
} | ||
|
||
static async login(username: String, password: String): Promise<[boolean, string]> { | ||
let requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user?username=" + username + "&password=" + password, requestOptions); | ||
|
||
if (response.ok) { | ||
cookie.save("token", response.headers.get("token")!, {}); | ||
} | ||
|
||
return [response.ok, await response.text()] | ||
} | ||
|
||
static async loginSession(): Promise<[boolean, string]> { | ||
if (cookie.load("token") == null) { | ||
return [false, ""]; | ||
} | ||
|
||
|
||
let headers = new Headers(); | ||
headers.append("token", cookie.load("token")); | ||
|
||
let requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow', | ||
headers: headers | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user", requestOptions); | ||
cookie.save("token", response.headers.get("token")!, {}); | ||
|
||
return [response.ok, await response.text()]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import cookie from "react-cookies"; | ||
|
||
export var SetLoggedInState: any = null; | ||
export var IsLoggedIn: boolean = !cookie.load("token") == undefined; | ||
|
||
export function UpdateSetLoggedInHook(hook: any) { | ||
SetLoggedInState = hook; | ||
SetLoggedInState = (loggedIn: boolean) => { | ||
hook(loggedIn); | ||
IsLoggedIn = loggedIn; | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {SERVER_URL} from "@/interface/api"; | ||
import {base} from "next/dist/build/webpack/config/blocks/base"; | ||
|
||
export class PictureAPI { | ||
static async uploadFile(file: File) { | ||
let formData = new FormData(); | ||
formData.append("file", file); | ||
|
||
let requestOptions: RequestInit = { | ||
method: 'POST', | ||
body: formData, | ||
redirect: 'follow' | ||
}; | ||
|
||
fetch(SERVER_URL + "/picture?name=" + file.name, requestOptions) | ||
.then(response => response.text()) | ||
.then(result => console.log(result)) | ||
.catch(error => console.log('error', error)); | ||
} | ||
} | ||
async function getBase64(file: File): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
let reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.onload = () => resolve(reader.result as string); | ||
reader.onerror = (error) => reject(error); | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import cookie from "react-cookies"; | ||
import {SERVER_URL} from "@/interface/api"; | ||
|
||
export class UserAPI { | ||
static async checkEmail(email: string): Promise<boolean> { | ||
//request | ||
var requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user/email/" + email, requestOptions); | ||
|
||
return await response.text() == "exists"; | ||
} | ||
|
||
static async creatUser(email: string, password: string, username: string, code: string): Promise<[boolean, string]> { | ||
|
||
let formData = new FormData(); | ||
formData.append("email", email); | ||
formData.append("password", password); | ||
formData.append("username", username); | ||
formData.append("code", code); | ||
|
||
let requestOptions: RequestInit = { | ||
method: 'POST', | ||
body: formData, | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user", requestOptions); | ||
let text = await response.text(); | ||
|
||
return [response.ok, text]; | ||
} | ||
|
||
static async login(username: String, password: String): Promise<[boolean, string]> { | ||
let requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow' | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user?email=" + username + "&password=" + password, requestOptions); | ||
|
||
if (response.ok) { | ||
cookie.save("token", response.headers.get("token")!, {}); | ||
} | ||
|
||
return [response.ok, await response.text()] | ||
} | ||
|
||
static async loginSession(): Promise<[boolean, string]> { | ||
if (cookie.load("token") == null) { | ||
return [false, ""]; | ||
} | ||
|
||
|
||
let headers = new Headers(); | ||
headers.append("token", cookie.load("token")); | ||
|
||
let requestOptions: RequestInit = { | ||
method: 'GET', | ||
redirect: 'follow', | ||
headers: headers | ||
}; | ||
|
||
let response = await fetch(SERVER_URL + "/user", requestOptions); | ||
cookie.save("token", response.headers.get("token")!, {}); | ||
|
||
return [response.ok, await response.text()]; | ||
} | ||
} |