PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Org.NerdBeers/Org.NerdBeers.Web/Modules/AuthenticationModule.cs

https://github.com/ToJans/NerdBeers
C# | 47 lines | 37 code | 10 blank | 0 comment | 3 complexity | 16c7a693ed1b1665eb2be36a53dbb3f0 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Nancy;
  6. using Org.NerdBeers.Web.Models;
  7. using Org.NerdBeers.Web.Services;
  8. using Nancy.Authentication.Forms;
  9. namespace Org.NerdBeers.Web.Modules
  10. {
  11. public class AuthenticationModule : NerdBeerModule
  12. {
  13. IAuthenticationService authenticationservice;
  14. public AuthenticationModule(IAuthenticationService authenticationservice,IDBFactory DBFactory)
  15. : base("/authentication",DBFactory)
  16. {
  17. this.authenticationservice = authenticationservice;
  18. Post["/login"] = x =>
  19. {
  20. var nerd = authenticationservice.GetLogin(Request.Form.Username, Request.Form.Password);
  21. if (nerd == null)
  22. return Response.AsRedirect("/login?msg=Invalid%20username%20or%20password");
  23. DateTime? expiry = null;
  24. if (this.Request.Form.RememberMe.HasValue)
  25. {
  26. expiry = DateTime.Now.AddDays(7);
  27. }
  28. Guid guid = Guid.Parse(nerd.Guid);
  29. return this.LoginAndRedirect(guid, expiry);
  30. };
  31. Get["/logout"] = x =>
  32. {
  33. return this.LogoutAndRedirect("/");
  34. };
  35. }
  36. }
  37. }