first commit
This commit is contained in:
46
app/auth/repository.py
Normal file
46
app/auth/repository.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.models import EmailVerificationToken, PasswordResetToken
|
||||
|
||||
|
||||
async def create_email_verification_token(db: AsyncSession, user_id: int, token: str, expires_at: datetime) -> None:
|
||||
db.add(
|
||||
EmailVerificationToken(
|
||||
user_id=user_id,
|
||||
token=token,
|
||||
expires_at=expires_at,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def get_email_verification_token(db: AsyncSession, token: str) -> EmailVerificationToken | None:
|
||||
result = await db.execute(select(EmailVerificationToken).where(EmailVerificationToken.token == token))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def delete_email_verification_tokens_for_user(db: AsyncSession, user_id: int) -> None:
|
||||
await db.execute(delete(EmailVerificationToken).where(EmailVerificationToken.user_id == user_id))
|
||||
|
||||
|
||||
async def create_password_reset_token(db: AsyncSession, user_id: int, token: str, expires_at: datetime) -> None:
|
||||
db.add(
|
||||
PasswordResetToken(
|
||||
user_id=user_id,
|
||||
token=token,
|
||||
expires_at=expires_at,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def get_password_reset_token(db: AsyncSession, token: str) -> PasswordResetToken | None:
|
||||
result = await db.execute(select(PasswordResetToken).where(PasswordResetToken.token == token))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def delete_password_reset_tokens_for_user(db: AsyncSession, user_id: int) -> None:
|
||||
await db.execute(delete(PasswordResetToken).where(PasswordResetToken.user_id == user_id))
|
||||
Reference in New Issue
Block a user