U
    ?i?                     @   s   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZmZmZ eddgd	Zejd
edeefeedddZedeefedddZedeeeefeeedddZdS )    )	APIRouterDependsHTTPExceptionstatus)Session)get_db)verify_passwordhash_passwordcreate_access_tokenget_current_user)User)LoginRequestTokenResponseChangePasswordRequestz	/api/authauth)prefixtagsz/login)response_model)bodydbc                 C   sX   | ttj| jk }|r.t| j|js<tt	j
ddtd|ji}t||jdS )NzInvalid username or passwordstatus_codedetailsub)access_tokenusername)queryr   filterr   firstr   passwordhashed_passwordr   r   HTTP_401_UNAUTHORIZEDr
   r   )r   r   usertoken r$   6/var/www/html/me.goteku.com/backend/app/routes/auth.pylogin   s    r&   z/mecurrent_userc                 C   s   d| iS )Nr   r$   r'   r$   r$   r%   me   s    r)   z/change-password)r   r(   r   c                 C   sr   | ttj|k }|r,t| j|js:tt	j
ddt| jdk rVtt	j
ddt| j|_|  ddiS )NzCurrent password is incorrectr      z*New password must be at least 6 charactersmessagezPassword changed successfully)r   r   r   r   r   r   Zcurrent_passwordr    r   r   HTTP_400_BAD_REQUESTlenZnew_passwordr	   commit)r   r(   r   r"   r$   r$   r%   change_password   s    r/   N)fastapir   r   r   r   Zsqlalchemy.ormr   Zapp.core.databaser   app.core.securityr   r	   r
   r   Zapp.models.userr   Zapp.schemas.authr   r   r   routerpostr&   getstrr)   r/   r$   r$   r$   r%   <module>   s$   