Vapor 3 GET route for sensitive data
up vote
2
down vote
favorite
Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:
protectedRouter.get("users", User.parameter, use: userController.user)
And method in UserController:
func user(_ req: Request) throws -> Future<User> {
return try req.parameters.next(User.self)
}
And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).
swift vapor vapor3
add a comment |
up vote
2
down vote
favorite
Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:
protectedRouter.get("users", User.parameter, use: userController.user)
And method in UserController:
func user(_ req: Request) throws -> Future<User> {
return try req.parameters.next(User.self)
}
And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).
swift vapor vapor3
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:
protectedRouter.get("users", User.parameter, use: userController.user)
And method in UserController:
func user(_ req: Request) throws -> Future<User> {
return try req.parameters.next(User.self)
}
And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).
swift vapor vapor3
Using numerous tutorials on Vapor 3 I've failed to figure out how I can edit output JSON, f.e. to get particular User object I create route:
protectedRouter.get("users", User.parameter, use: userController.user)
And method in UserController:
func user(_ req: Request) throws -> Future<User> {
return try req.parameters.next(User.self)
}
And it, of course, sends everything that inside the User object, including email and hashed password. Great. How can I avoid this? I mean I want to send only public information about the user (name, nick, id etc...).
swift vapor vapor3
swift vapor vapor3
asked Nov 5 at 1:38
faviomob
2,53712125
2,53712125
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.
A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,
extension User {
struct Public: Content { ... }
func makePublic() -> Public { ... }
}
Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.
You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.
A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,
extension User {
struct Public: Content { ... }
func makePublic() -> Public { ... }
}
Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.
You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.
add a comment |
up vote
4
down vote
accepted
Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.
A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,
extension User {
struct Public: Content { ... }
func makePublic() -> Public { ... }
}
Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.
You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.
A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,
extension User {
struct Public: Content { ... }
func makePublic() -> Public { ... }
}
Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.
You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.
Create a separate struct representing your desired output structure. Conform that struct to Content. Whenever you return your User, convert it to that struct first. Adding an extension to User that does this is nice for convenience.
A common pattern emerging is to nest this struct inside the Model calling it Public. i.e.,
extension User {
struct Public: Content { ... }
func makePublic() -> Public { ... }
}
Your routes would then return User.Public instead of User. Note that this pattern is also useful in reverse, for creating a separate "input" representation for your User.
You can read more about this in Vapor's docs at Vapor → Content → Dynamic Properties.
answered Nov 5 at 19:09
tanner0101
3,0951233
3,0951233
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147253%2fvapor-3-get-route-for-sensitive-data%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password