Skip to content

Instantly share code, notes, and snippets.

@qwe2
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qwe2/bc1aa6fdbc8cea035297 to your computer and use it in GitHub Desktop.
Save qwe2/bc1aa6fdbc8cea035297 to your computer and use it in GitHub Desktop.
Protect Content with WebSharper.Warp
#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