Create a token

Problem

You need to create a token in the current database.

Solution

Use the Login function:

try
{
    Value result = await client.Query(
        Login(
            Ref(Collection("characters"), "181388642114077184"),
            Obj("password", "abracadabra")
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "309834562535424512", collection = RefV(id = "tokens")),ts: LongV(1631740114630000),instance: RefV(id = "181388642114077184", collection = RefV(id = "characters", collection = RefV(id = "collections"))),secret: StringV(fnEETMDknxACAARMwOIEYAYAhr4xpyR13i1E4TokiFwAYsPz0LE))
result, err := client.Query(
	f.Login(
		f.Ref(f.Collection("characters"), "181388642114077184"),
		f.Obj{"password": "abracadabra"},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[instance:{181388642114077184 0xc000109bc0 0xc000109bc0 <nil>} ref:{309834289381376512 0xc000109a10 0xc000109a10 <nil>} secret:fnEETMClBdACAARMwKQSgAIAKgw15g2WZCeCm3bkvhfEzvWDmpI ts:1631739854140000]
System.out.println(
    client.query(
        Login(
            Ref(Collection("characters"), Value("181388642114077184")),
            Obj("password", Value("abracadabra"))
        )
    ).get());
{ref: ref(id = "309834736999596544", collection = ref(id = "tokens")), ts: 1631740281020000, instance: ref(id = "181388642114077184", collection = ref(id = "characters", collection = ref(id = "collections"))), secret: "fnEETMENPfACAARMwQoysAYAe-Q6mGG3S3glkNI8g0MICqKvQzA"}
client.query(
  q.Login(
    q.Ref(q.Collection('characters'), '181388642114077184'),
    { password: 'abracadabra' },
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Tokens(), "309833836335727104"),
  ts: 1631739422080000,
  instance: Ref(Collection("characters"), "181388642114077184"),
  secret: 'fnEETMA7ijACAARMwDsk0AYAu4E51VmH1w-Xm7qk8T6eJcbinkI'
}
result = client.query(
  q.login(
    q.ref(q.collection("characters"), "181388642114077184"),
    {"password": "abracadabra"}
  )
)
print(result)
{'ref': Ref(id=309834127742337536, collection=Ref(id=tokens)), 'ts': 1631739699980000, 'instance': Ref(id=181388642114077184, collection=Ref(id=characters, collection=Ref(id=collections))), 'secret': 'fnEETMB_Y2ACAARMwH7g0AYAw183rIQzi0_4AnRBYTUf15JxUg8'}
try {
  println(Await.result(
    client.query(
      Login(
        Ref(Collection("characters"), "181388642114077184"),
        Obj("password" -> "abracadabra")
      )
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "309835097747489280", collection = ref(id = "tokens")), ts: 1631740625050000, instance: ref(id = "181388642114077184", collection = ref(id = "characters", collection = ref(id = "collections"))), secret: "fnEETMFhPDACAARMwVc-cAYA7kLx0TP3XLC-FcGuilH8LENemGk"}
Login(
  Ref(Collection("characters"), "181388642114077184"),
  { password: "abracadabra" },
)
{
  ref: Ref(Tokens(), "309833029179671040"),
  ts: 1631738652300000,
  instance: Ref(Collection("characters"), "181388642114077184"),
  secret: 'fnEETL9_m_ACAARMv36xQAYA0e8RzC8kR9TQDLEM5tEpq79-w_M'
}

You can also create one directly, if the identity document does not have any credentials defined:

try
{
    Value result = await client.Query(
        Create(
            Tokens(),
            Obj(
                "instance", Ref(
                    Collection("characters"),
                    "181388642114077184"
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "309834558324343296", collection = RefV(id = "tokens")),ts: LongV(1631740110630000),instance: RefV(id = "181388642114077184", collection = RefV(id = "characters", collection = RefV(id = "collections"))),secret: StringV(fnEETMDjpBACAARMwOIEYAYAO2NCSaJssZSdlcXUlgZmw70FnfE))
result, err := client.Query(
	f.Create(
		f.Tokens(),
		f.Obj{
			"instance": f.Ref(
				f.Collection("characters"),
				"181388642114077184",
			),
		},
	),
)

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[instance:{181388642114077184 0xc000180660 0xc000180660 <nil>} ref:{309834288701899264 0xc0001804b0 0xc0001804b0 <nil>} secret:fnEETMCk3VACAARMwKQSgAIAs-uMMiwCOxE2LCTVOI0ssKbxa5U ts:1631739853490000]
System.out.println(
    client.query(
        Create(
            Tokens(),
            Obj(
                "instance", Ref(
                    Collection("characters"),
                    "181388642114077184"
                )
            )
        )
    ).get());
{ref: ref(id = "309834731615158784", collection = ref(id = "tokens")), ts: 1631740275890000, instance: ref(id = "181388642114077184", collection = ref(id = "characters", collection = ref(id = "collections"))), secret: "fnEETMEL_QACAARMwQoysAYA1P90wsUVdPW8JfT3PeM9-wm3umE"}
client.query(
  q.Create(
    q.Tokens(),
    {
      instance: q.Ref(
        q.Collection('characters'),
        '181388642114077184',
      ),
    },
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Tokens(), "309833836087214592"),
  ts: 1631739421850000,
  instance: Ref(Collection("characters"), "181388642114077184"),
  secret: 'fnEETMA7e2ACAARMwDsk0AYATF1jLijRmK7HYAZDxe1snLrPWJs'
}
result = client.query(
  q.create(
    q.tokens(),
    {
      "instance": q.ref(
        q.collection('characters'),
        '181388642114077184'
      )
    }
  )
)
print(result)
{'ref': Ref(id=309834127428813312, collection=Ref(id=tokens)), 'ts': 1631739699690000, 'instance': Ref(id=181388642114077184, collection=Ref(id=characters, collection=Ref(id=collections))), 'secret': 'fnEETMB_ULACAARMwH7g0AYASH2StILhxeWQQeZ709pMTxfYU-o'}
try {
  println(Await.result(
    client.query(
      Create(
        Tokens(),
        Obj(
          "instance" -> Ref(
            Collection("characters"),
            "181388642114077184"
          )
        )
      )
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "309835076307255808", collection = ref(id = "tokens")), ts: 1631740604610000, instance: ref(id = "181388642114077184", collection = ref(id = "characters", collection = ref(id = "collections"))), secret: "fnEETMFcPkACAARMwVc-cAYAbSzIyhS84sfGkI4JPPfn1WOc4Kk"}
Create(
  Tokens(),
  {
    instance: Ref(Collection('characters'), '181388642114077184')
  }
)
{
  ref: Ref(Tokens(), "309833028300964352"),
  ts: 1631738651480000,
  instance: Ref(Collection("characters"), "181388642114077184"),
  secret: 'fnEETL9_Z5ACAARMv36xQAYAsGq8dFWhaBsypurs5u8qAN6Zfm8'
}
The token’s secret is only ever shown once, in the response from the Login function. If you lose the secret, delete the token and create a new one.

More information

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!