|
| 1 | +// EXAMPLE: cmds_cnxmgmt |
| 2 | +// HIDE_START |
| 3 | +package example_commands_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +// HIDE_END |
| 13 | + |
| 14 | +func ExampleClient_cmd_auth() { |
| 15 | + ctx := context.Background() |
| 16 | + |
| 17 | + rdb := redis.NewClient(&redis.Options{ |
| 18 | + Addr: "localhost:6379", |
| 19 | + Password: "", // no password docs |
| 20 | + DB: 0, // use default DB |
| 21 | + }) |
| 22 | + // REMOVE_START |
| 23 | + |
| 24 | + // REMOVE_END |
| 25 | + // STEP_START auth1 |
| 26 | + // REMOVE_START |
| 27 | + rdb.ConfigSet(ctx, "requirepass", "temp_pass") |
| 28 | + // REMOVE_END |
| 29 | + authResult1, err := rdb.Conn().Auth(ctx, "temp_pass").Result() |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | + |
| 35 | + fmt.Println(authResult1) // >>> OK |
| 36 | + |
| 37 | + authResult2, err := rdb.Conn().AuthACL( |
| 38 | + ctx, "default", "temp_pass", |
| 39 | + ).Result() |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Println(authResult2) // >>> OK |
| 46 | + // REMOVE_START |
| 47 | + rdb.ConfigSet(ctx, "require_pass", "") |
| 48 | + // REMOVE_END |
| 49 | + // STEP_END |
| 50 | + |
| 51 | + // STEP_START auth2 |
| 52 | + authResult3, err := rdb.Conn().AuthACL(ctx, |
| 53 | + "test_user", "strong_password", |
| 54 | + ).Result() |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + fmt.Println(err) |
| 58 | + } |
| 59 | + |
| 60 | + fmt.Println(authResult3) // >>> OK |
| 61 | + // STEP_END |
| 62 | + |
| 63 | + // Output: |
| 64 | + // OK |
| 65 | + // OK |
| 66 | + // WRONGPASS invalid username-password pair or user is disabled. |
| 67 | +} |
0 commit comments