@expressive-analytics/deep-thought-authentication
Version:
Typescript conversion of Deep Thought Authentication
51 lines (45 loc) • 1.29 kB
text/typescript
import {DTQueryBuilder} from '@expressive-analytics/deep-thought-js'
import {DTHTTPError, DTService, DTSession} from '@expressive-analytics/deep-thought-service'
import {DTUserModel} from './DTUser'
export class DTAuthenticationService extends DTService {
actionAuthenticate():DTUserModel{
const session = DTSession.shared();
const u = this.castUser(
this.db.filter({
"alias":[this.db.ilike,this.params.stringParam("alias")],
"is_active":1
})
) as DTUserModel
if(u.verifyPassword(this.params.stringParam("password"))){
session.$set("pvd_user_id",u.$get("id"))
return u;
} else{
this.response.error(401)
session.unset("pvd_user_id")
return undefined;
}
}
actionPasswordResetToken(){
throw new Error("method not implemented!")
}
actionResetPassword(){
throw new Error("method not implemented!")
}
currentUserID():number{
const s = DTSession.shared()
return s.$get("pvd_user_id")
}
actionCurrentUser(){
const id = this.currentUserID()
try{
this.castUser(this.db.filter({id}))
}catch(e){}
return null
}
castUser(qb?:DTQueryBuilder):DTUserModel{
return DTUserModel.query(qb) as DTUserModel
}
actionLogout(){
DTSession.destroy()
}
}