1- use axum:: {
2- http:: StatusCode ,
3- response:: { IntoResponse , Response } ,
4- Json ,
5- } ;
1+ use axum:: Json ;
62use serde:: { Deserialize , Serialize } ;
7- use shield:: User ;
3+ use shield:: { EmailAddress , ShieldError , User } ;
84
9- use crate :: extract:: ExtractUser ;
5+ use crate :: {
6+ error:: { ErrorBody , RouteError } ,
7+ extract:: ExtractUser ,
8+ } ;
109
1110#[ derive( Deserialize , Serialize ) ]
1211#[ cfg_attr( feature = "utoipa" , derive( utoipa:: ToSchema ) ) ]
1312#[ cfg_attr( feature = "utoipa" , schema( as = User ) ) ]
1413#[ serde( rename_all = "camelCase" ) ]
15- struct UserBody {
14+ pub struct UserBody {
1615 id : String ,
1716 name : Option < String > ,
17+ email_addresses : Vec < EmailAddress > ,
1818}
1919
2020impl UserBody {
21- async fn new < U : User > ( user : U ) -> Self {
22- // TODO: Include email addresses.
23- // let email_addresses = user.email_addresses().await;
21+ async fn new < U : User > ( user : U ) -> Result < Self , ShieldError > {
22+ let email_addresses = user. email_addresses ( ) . await ?;
2423
25- Self {
24+ Ok ( Self {
2625 id : user. id ( ) ,
2726 name : user. name ( ) ,
28- }
27+ email_addresses,
28+ } )
2929 }
3030}
3131
@@ -38,15 +38,15 @@ impl UserBody {
3838 description = "Get the current user account." ,
3939 responses(
4040 ( status = 200 , description = "Current user account." , body = UserBody ) ,
41- ( status = 401 , description = "No account signed in." ) ,
42- ( status = 500 , description = "Internal server error." ) ,
41+ ( status = 401 , description = "No account signed in." , body = ErrorBody ) ,
42+ ( status = 500 , description = "Internal server error." , body = ErrorBody ) ,
4343 )
4444 )
4545) ]
46- pub async fn user < U : User > ( ExtractUser ( user ) : ExtractUser < U > ) -> Response {
47- // TODO: Send JSON error using some util.
48- match user {
49- Some ( user) => Json ( UserBody :: new ( user ) . await ) . into_response ( ) ,
50- None => ( StatusCode :: UNAUTHORIZED , "Unauthorized" ) . into_response ( ) ,
51- }
46+ pub async fn user < U : User > (
47+ ExtractUser ( user ) : ExtractUser < U > ,
48+ ) -> Result < Json < UserBody > , RouteError > {
49+ let user = user . ok_or ( ShieldError :: Unauthorized ) ? ;
50+
51+ Ok ( Json ( UserBody :: new ( user ) . await ? ) )
5252}
0 commit comments