LoginSignup
6
0

More than 1 year has passed since last update.

[Golang]GinでCookieをセットする方法

Last updated at Posted at 2021-10-07

はじめに

GinでCookieをセットするやり方でハマったので、記録します。

やり方

    // Cookieをセット
    cookie := new(http.Cookie)
    cookie.Value = token //Cookieに入れる値

    // samesiteをnonemodeにする
    c.SetSameSite(http.SameSiteNoneMode)

    // ローカルの場合
    if os.Getenv("ENV") == "local" {
        log.Println("cookieをセットする")
        c.SetCookie("jwt", cookie.Value, 3600, "/", "localhost", true, true)
    }

       // 本番環境の場合
    if os.Getenv("ENV") == "production" {
        log.Println("productionでcookieをセットする")
        c.SetCookie("jwt", cookie.Value, 3600, "/", "your_domain", true, true)
    }

SetCookieについて

SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

ハマったところ

  • samesiteをnoneモードにしないといけないところ
  • 環境に合わせて、ドメインを入力するところ

終わりに

なかなかGinに関する情報って出てこなかったので、苦労しました、、

参考記事

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0