45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
# Copyright 2026 zhaoxi826
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from fastapi import APIRouter, Request
|
|
from pydantic import BaseModel
|
|
from pretor.utils.access import Accessor
|
|
|
|
auth_router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
|
|
|
class UserRegister(BaseModel):
|
|
user_name: str
|
|
password: str
|
|
|
|
@auth_router.post("/register")
|
|
async def create_user(user_register: UserRegister, request: Request):
|
|
postgres_database = request.app.state.postgres_database
|
|
hashed_password = Accessor.hash_password(user_register.password)
|
|
user = await postgres_database.auth_database.add_user.remote(user_register.user_name, hashed_password)
|
|
return {"message": "success", "user_id": user.user_id}
|
|
|
|
class UserLogin(BaseModel):
|
|
user_name: str
|
|
password: str
|
|
|
|
@auth_router.post("/login")
|
|
async def login_user(user_login: UserLogin, request: Request):
|
|
postgres_database = request.app.state.postgres_database
|
|
user = postgres_database.auth_database.login_user.remote(user_login.user_name)
|
|
if user.user_name != user_login.user_name:
|
|
pass
|
|
token = Accessor.login_hashed_password(user, user_login.password)
|
|
return {"message":"success", "token":token}
|
|
|