add admin login
This commit is contained in:
@@ -1,37 +1,53 @@
|
||||
import { DataTypes, Sequelize } from 'sequelize';
|
||||
import { DataTypes } from 'sequelize';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { building, dev } from '$app/environment';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { BeforeCreate, BeforeUpdate, Column, Model, Sequelize, Table } from 'sequelize-typescript';
|
||||
|
||||
@Table({ modelName: 'user' })
|
||||
export class User extends Model {
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare firstname: string;
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare lastname: string;
|
||||
@Column({ type: DataTypes.DATE, allowNull: false })
|
||||
declare birthday: Date;
|
||||
@Column({ type: DataTypes.STRING })
|
||||
declare telephone: string;
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare username: string;
|
||||
@Column({ type: DataTypes.ENUM('java', 'bedrock', 'cracked'), allowNull: false })
|
||||
declare playertype: 'java' | 'bedrock' | 'cracked';
|
||||
@Column({ type: DataTypes.STRING })
|
||||
declare password: string;
|
||||
@Column({ type: DataTypes.UUIDV4 })
|
||||
declare uuid: string;
|
||||
}
|
||||
|
||||
@Table({ modelName: 'admin' })
|
||||
export class Admin extends Model {
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare username: string;
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare password: string;
|
||||
@Column({ type: DataTypes.BIGINT, allowNull: false })
|
||||
declare permissions: number;
|
||||
|
||||
@BeforeCreate
|
||||
@BeforeUpdate
|
||||
static hashPassword(instance: Admin) {
|
||||
if (instance.password != null) {
|
||||
instance.username = bcrypt.hashSync(instance.password, 10);
|
||||
}
|
||||
}
|
||||
|
||||
validatePassword(password: string): boolean {
|
||||
return bcrypt.compareSync(password, this.password);
|
||||
}
|
||||
}
|
||||
|
||||
export const sequelize = new Sequelize(building ? 'sqlite::memory:' : env.DATABASE_URI, {
|
||||
// only log sql queries in dev mode
|
||||
logging: dev ? console.log : false
|
||||
});
|
||||
|
||||
export const User = sequelize.define('user', {
|
||||
firstname: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
lastname: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
birthday: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false
|
||||
},
|
||||
telephone: DataTypes.STRING,
|
||||
username: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
playertype: {
|
||||
type: DataTypes.ENUM('java', 'bedrock', 'cracked'),
|
||||
allowNull: false
|
||||
},
|
||||
password: DataTypes.TEXT,
|
||||
uuid: {
|
||||
type: DataTypes.UUIDV4,
|
||||
allowNull: false
|
||||
}
|
||||
logging: dev ? console.log : false,
|
||||
models: [User, Admin]
|
||||
});
|
||||
|
||||
11
src/lib/server/session.ts
Normal file
11
src/lib/server/session.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
const sessions: string[] = [];
|
||||
|
||||
export function addSession(): string {
|
||||
const session = 'AAA';
|
||||
sessions.push(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
export function hasSession(session: string): boolean {
|
||||
return sessions.find((v) => v == session) != undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user