Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F995498
users.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
users.ts
View Options
import
Router
from
"@koa/router"
;
import
type
Policy
from
"../../chip-types/policy"
;
import
{
Collection
,
FieldTypes
,
Policies
,
Context
,
ActionName
,
Field
,
}
from
"../../main"
;
import
{
BadContext
}
from
"../../response/errors"
;
import
SecureHasher
from
"../../utils/secure-hasher"
;
export
default
class
Users
extends
Collection
{
fields
=
{
username
:
new
FieldTypes
.
Username
(),
password
:
new
FieldTypes
.
Password
(),
}
as
{
username
:
Field
;
password
:
Field
;
};
defaultPolicy
:
Policy
=
new
Policies
.
Themselves
();
policies
=
{
create
:
new
Policies
.
Super
(),
show
:
new
Policies
.
Themselves
(),
}
as
Partial
<
{
[
a
in
ActionName
]
:
Policy
}
>
;
getRouter
()
{
const
router
=
new
Router
();
router
.
get
(
"/me"
,
async
(
ctx
)
=>
{
if
(
typeof
ctx
.
$context
.
user_id
!==
"string"
)
{
throw
new
BadContext
(
"You're not logged in!"
);
}
const
user
=
this
.
list
(
ctx
.
$context
)
.
ids
([
ctx
.
$context
.
user_id
])
.
setParams
(
ctx
.
query
);
ctx
.
body
=
(
await
user
.
fetch
()).
serialize
()
as
Record
<
string
,
unknown
>
;
});
const
super_router
=
super
.
getRouter
();
router
.
use
(
super_router
.
routes
(),
super_router
.
allowedMethods
());
return
router
;
}
static
async
passwordMatches
(
context
:
Context
,
username
:
string
,
password
:
string
)
{
const
[
user
]
=
await
context
.
app
.
Datastore
.
find
(
"users"
,
{
"username.safe"
:
username
,
});
if
(
!
user
||
!
user
.
password
)
{
return
false
;
}
return
await
SecureHasher
.
matches
(
password
,
user
.
password
as
string
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Mon, Dec 23, 06:41 (13 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556838
Default Alt Text
users.ts (1 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment