vapor-community / auth-example Goto Github PK
View Code? Open in Web Editor NEWAuthentication example for Vapor 1.0.
License: MIT License
Authentication example for Vapor 1.0.
License: MIT License
I tried to follow this example. My project built with vapor 2, but this example is built with Vapor 1.x(as most of the vapor-community examples).
Could anyone update this example to use vapor 2.x?
I am trying to create a login route. I have successfully created the register portion, but am struggling to determine what has gone wrong on the login side of things.
I have the protect and auth middleware setup on the drop, as well as in the middleware.json
file. Here is the code for my login route:
users.post("login") { req in
guard let username = req.data["username"]?.string else {
throw Abort.badRequest
}
guard let password = req.data["password"]?.string else {
throw Abort.badRequest
}
let creds = UsernamePassword(username: username, password: password)
try req.auth.login(creds)
let u = try req.user()
return try JSON(node: ["message": "\(u.name) Logged in."])
}
Which is all wrapped within a group called users
.
Here's the weird thing. It always works the first time after clearing the cache, but then fails the second time (after succeeding the first time) and results in an error:
Uncaught Error: AuthError.noSubject. Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment.
What am I doing wrong?? Here is my authenticate
function on my User
model:
static func authenticate(credentials: Credentials) throws -> Auth.User {
let user: User?
switch credentials {
case let usernamePassword as UsernamePassword:
print(usernamePassword.password + usernamePassword.username)
user = try User.query().filter("email", usernamePassword.username).filter("password", usernamePassword.password).first()
default:
throw Abort.custom(status: .badRequest, message: "Invalid credentials.")
}
guard let u = user else {
throw Abort.custom(status: .badRequest, message: "User not found")
}
return u
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.