2016-09-23 43 views
2

我有一個處理OAuth回調請求的Web部件。如何在Suave userstate中存儲數據?

從API獲取令牌和用戶ID後,我想將其存儲在會話狀態中。但是在後續請求中讀取會話時,我只看到「Suave.Auth」鍵的值。

下面是OAuth的回調我的網絡的一部分:

path "/oauth" >=> context (fun ctx -> 
     let req = ctx.request 
     match (req.queryParam "code", req.queryParam "error") with 
     | Choice1Of2 code, _ -> 
      let id = completeOAuth code 
      Authentication.authenticated Session false 
      >=> Writers.setUserData "user-id" id 
      >=> Redirection.redirect "/view" 
     | _, Choice1Of2 error -> RequestErrors.UNAUTHORIZED error) 

我怎樣才能確保「用戶ID」值是在這一個後其他請求的會議?

回答

2

Writers.setUserData將數據存儲在僅在請求期間存在的映射中。

要存儲跨請求的數據,您需要使用這樣的statefulForSession

let app = 
    statefulForSession >=> context (fun x -> 
    match HttpContext.state x with 
    | None -> 
     // restarted server without keeping the key; set key manually? 
     let msg = "Server Key, Cookie Serialiser reset, or Cookie Data Corrupt, " 
     + "if you refresh the browser page, you'll have gotten a new cookie." 
     OK msg 
    | Some store -> 
     match store.get "counter" with 
     | Some y -> 
     store.set "counter" (y + 1) >=> OK (sprintf "Hello %d time(s)" (y + 1)) 
     | None -> 
     store.set "counter" 1 >=> OK "First time")