-
-
Save jacqueline-homan/85057c8b3a1d425387df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#I "../packages/Owin.1.0/lib/net40" | |
#I "../packages/Microsoft.Owin.3.0.1/lib/net45" | |
#I "../packages/Microsoft.Owin.Host.HttpListener.3.0.1/lib/net45" | |
#I "../packages/Microsoft.Owin.Hosting.3.0.1/lib/net45" | |
#I "../packages/Microsoft.Owin.FileSystems.3.0.1/lib/net45" | |
#I "../packages/Microsoft.Owin.StaticFiles.3.0.1/lib/net45" | |
#I "../packages/WebSharper.3.2.8.170/lib/net40" | |
#I "../packages/WebSharper.Compiler.3.2.4.170/lib/net40" | |
#I "../packages/WebSharper.Owin.3.2.6.83/lib/net45" | |
#load "../packages/WebSharper.Warp.3.2.10.13/tools/reference.fsx" | |
open WebSharper | |
open WebSharper.Sitelets | |
open WebSharper.Html.Server | |
type Endpoints = | |
| [<EndPoint "GET /">] Home | |
| [<EndPoint "GET /login">] Login | |
| [<EndPoint "GET /locked">] Locked | |
let Protect content verify redirectAction = | |
Content.CustomContentAsync <| fun ctx -> | |
async { | |
let! user = ctx.UserSession.GetLoggedInUser () | |
if verify user then | |
return! Content.ToResponseAsync content ctx | |
else | |
return! Content.ToResponseAsync (Content.RedirectTemporary redirectAction) ctx | |
} | |
let MySite = | |
Warp.CreateApplication (fun ctx endpoint -> | |
let (=>) label endpoint = A [HRef (ctx.Link endpoint)] -< [Text label] | |
match endpoint with | |
| Endpoints.Home -> | |
Warp.Page( | |
Body = | |
[ | |
H1 [Text "Hello world!"] | |
"Login" => Endpoints.Login | |
"Protected" => Endpoints.Locked | |
] | |
) | |
| Endpoints.Locked -> | |
Protect | |
<| Warp.Page( | |
Body = | |
[ | |
P [Text "This the protected page"] | |
] | |
) | |
<| Option.isSome // only allow logged in users | |
<| Home | |
| Endpoints.Login -> | |
Protect | |
<| Content.CustomContentAsync (fun ctx -> | |
// Some random login logic | |
async { | |
do! ctx.UserSession.LoginUser <| System.Guid.NewGuid().ToString() | |
return! Content.ToResponseAsync (Content.RedirectTemporary Endpoints.Home) ctx | |
}) | |
<| Option.isNone // redirect user back to the homepage if they are already logged in | |
<| Endpoints.Home | |
) | |
[<EntryPoint>] | |
do Warp.RunAndWaitForInput(MySite) |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment